如何使用Python“requests”模块检查页面是否使用“GET”方法或“POST”方法。
我期望输出为 True 或 False,或者 GET 或 Post。
预期代码:
import requests
url = f"www.get_example.com"
response = requests.get(url)
if response.check_get == True:
print("get")
我找到了答案:
#!/usr/bin/env python3
import requests
def check_request_type(url):
global method
try:
get_response = requests.get(url)
if get_response.status_code == 200:
return "GET"
except requests.RequestException as e:
#print(f"GET request failed: {e}")
pass
try:
post_response = requests.post(url)
if post_response.status_code == 200:
return "POST"
except requests.RequestException as e:
#print(f"POST request failed: {e}")
pass
return "GET/POST"
# Example usage
url = f'https://signup.com/login/'
result = check_request_type(url)
print(result)
如果要从页面读取数据,大多数时候是GET