标签:请求头 rom res pip format text 开发 log sof
pip install requests
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)
以微信公众号接口品台为例,其中appid和secret值获取方式为:
1、进入微信公众平台开发者文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
2、进入开始开发-接口测试号申请菜单,点击进入微信公众帐号测试号申请系统
#模拟带参数的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模拟请求头
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请求
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‘))
requests基础(一)-requests模拟get请求、post请求
标签:请求头 rom res pip format text 开发 log sof
原文地址:https://www.cnblogs.com/lvhuayan/p/14901368.html