码迷,mamicode.com
首页 > 其他好文 > 详细

requests基础(一)-requests模拟get请求、post请求

时间:2021-06-19 18:49:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:请求头   rom   res   pip   format   text   开发   log   sof   

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请求

以微信公众号接口品台为例,其中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模拟请求头

#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))

 

requests基础(一)-requests模拟get请求、post请求

标签:请求头   rom   res   pip   format   text   开发   log   sof   

原文地址:https://www.cnblogs.com/lvhuayan/p/14901368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!