直接上代码示例。(具体看注释)
import requests
import json
def GET(url):
#get请求
req = requests.get(url)
#输出状态码
print(req.status_code)
#输出返回内容
print(req.text)
def POST(url,cookie):
#post参数
post_data = "a=1&b=2"
#post 请求头
headers = {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Mobile Safari/537.36",
"Cookie": cookie}
req = requests.post(url,data=post_data,headers=headers)
#注意:如果post_data是字典json形式的,如下,参数一般用json=
post_data = {"a":1,"b":2}
req = requests.post(url,json=post_data,headers=headers)
#如果req返回的json数据,则可以用json的一个函数转换
data = json.loads(req.text)
print(req.status_code)
print(req.text)