requests基礎(一)-requests模擬get請求、post請求
阿新 • • 發佈:2021-06-19
requests安裝
pip install requests
requests模擬get請求
response.content 是二進位制模式,通常需要轉換成UTF-8模式,否則會亂碼
#requests模擬get請求、 import requests response=requests.get('https://www.taobao.com/') # 方式一: # print(response.content.decode('utf-8')) #response.content 二進位制模式 # 方式二 response.encoding='utf-8' print(response.text)
requests模擬帶引數的get請求
#模擬帶引數的get請求
import requests
#方式一
# response=requests.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx93fc8716b3795e89&secret=1ff879affa4d6c7cddc27b2e99406988')
# print(response.content.decode('utf-8'))
# 方式二
get_param_data={
"grant_type ":"client_credential",
"appid":"wx93fc8716b3795e89",
"secret":"1ff879affa4d6c7cddc27b2e99406988"
}
response=requests.get('https://api.weixin.qq.com/cgi-bin/token',get_param_data)
print(response.content.decode('utf-8'))
requests模擬請求頭
#requests模擬請求頭
import requests
get_param_data={
"wd":"天天向上 "
}
head_info={
"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Mobile Safari/537.36",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"zh-CN,zh;q=0.9"
}
response=requests.get(url='https://www.baidu.com/s',params=get_param_data,headers=head_info)
print(response.content.decode('utf-8'))
requests模擬post請求
# requests模擬post請求
import requests
import json
get_param_data={
'access_token':'45_p4NSp2xHLUwWDgGhBItGJfvtax5Z1jiyikMxA3LvAKIIoaAbwgdvFLbtrToc7Fzj1lg1ZaaVGaKN9g01PzkKhyLK4b4mIh3Y_k7Z61anAXMzpmzGIUvwifLhmCfDOkTgzE4GW5S7cJurTK8SMNViAGATUC'
}
post_param_data={"tag": {"id":103,"name":"上海話77"}}
header_info={'Content-Type':'application/json'}
response=requests.post(url='https://api.weixin.qq.com/cgi-bin/tags/update',
params=get_param_data,
# data=json.dumps(post_param_data), #json.dumps :把字典轉換成字串
json=post_param_data,
headers=header_info
)
print(response.content.decode('utf-8'))