Python网络请求
Posted 黑。白。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python网络请求相关的知识,希望对你有一定的参考价值。
1、网络请求 urllib 自带模块:
1、get 请求
2、post 请求
3、url 里面传参
4、body、josn
5、cookie
6、header
7、上传文件
8、k-v
from urllib.request import urlopen
from urllib.parse import urlencode,urljoin,quote,quote_plus,unquote,unquote_plus
urljoin 拼接url
quote,quote_plus 就是把传的参数转换成url编码
unquote,unquote_plus url编码进行解码,一般用plus,比较全一点
# host = "http://api.nnzhp.cn/?stu_name=小黑"
# print(quote_plus(host)) #url编码
# print(quote(host))
url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=%E4%BD%A0%E5%A5%BD%E6%96%BD%E5%B7%A5%E9%98%9F&rsv_spt=1&oq=sdgsdsdg&rsv_pq=cbca1b490021d566&rsv_t=83a3dvr2MgkSUHYndjTkUaly8mj0O2gEaU6FiQmNXisrrzrJ0W3nU6KvAPnPtdK%2FyfiU&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=23&rsv_sug1=10&rsv_sug7=101&rsv_sug2=0&rsv_btype=t&inputT=4151&rsv_sug4=4151"
# url = urljoin(host,\'/api/login\')
print(unquote(url))
print(unquote_plus(url))
# url = "http://api.nnzhp.cn/api/user/stu_info"
data = {"stu_name":"xiaohei","age":18}
# print(urlencode(data))
# # req = urlopen(url,urlencode(data).encode() ) #post请求,url后面没有参数的话,就是get请求
# req = urlopen(url+\'?\'+urlencode(data))
# print(req.read().decode())
2、网络请求 requests模块
# url = "http://api.nnzhp.cn/api/user/login"
# data = {"username":"niuhanyang","passwd":"aA123456"}
# data2 = {"version":1}
# req = requests.post(url,params=data2,data=data)
# print(req.url) #查看请求发出去的url
#http://api.nnzhp.cn/api/user/login?version=1
#params参数是在url里面的
#data参数是在body里面的
1、发送get、post请求
req = requests.post(url,data,cookies=cookie) 发送post请求
req = requests.get(url,data) 发送get请求
2、发送请求传json格式
# url = "http://api.nnzhp.cn/api/user/add_stu"
# data = {
# "grade": "飞马座",
# "phone": "22345678992",
# "name":"哈哈哈哈"
# }
# req = requests.post(url,json=data) 发送请求传json格式数据
3、发送请求传cookie
# url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
# # data = {
# # "gc": 180208520,
# # "st": "21",
# # "end": "41",
# # "sort": 0,
# # "bkn": "1391997515"
# # }
# # cookie = {\'RK\': \'XYYhgDpPfy\', \'ptcz\': \'a5b23b4f93bc2f304119957286b77b21be95814358d482ef0a2bf216733e57ba\', \'pgv_pvid\': \'5931324545\', \'uin\': \'o0511402865\', \'skey\': \'@HHempil2m\', \'p_uin\': \'o0511402865\', \'pt4_token\': \'tkeLBl-znMjpBJxIv1Chj*kLx7p0dsh4BkZfF39cpDk_\', \'p_skey\': \'OS7gS9Zw8*r*aIpqd5-S5BUo65EJMNWRkLLMO1Vpu5o_\', \'traceid\': \'5b35fe5632\'}
# #
# # req = requests.post(url,data,cookies=cookie) 发送请求时传cookie
4、发送请求传header
# url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
# data = {
# "gc": 180208520,
# "st": "21",
# "end": "41",
# "sort": 0,
# "bkn": "1391997515"
# }
# header = {
# "cookie":"RK=XYYhgDpPfy; ptcz=a5b23b4f93bc2f304119957286b77b21be95814358d482ef0a2bf216733e57ba; pgv_pvid=5931324545; uin=o0511402865; skey=@HHempil2m; p_uin=o0511402865; pt4_token=tkeLBl-znMjpBJxIv1Chj*kLx7p0dsh4BkZfF39cpDk_; p_skey=OS7gS9Zw8*r*aIpqd5-S5BUo65EJMNWRkLLMO1Vpu5o_; traceid=5b35fe5632",
# "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
# }
# req = requests.post(url,data,headers=header)
5、上传文件
# url = "http://api.nnzhp.cn/api/file/file_upload"
# data = {"file":open("a.xls",\'rb\')}
# req = requests.post(url,files=data)
# print(req.json()) #返回字典,如果接口返回的不是json,那会报错
6、下载文件
url = "http://aliimg.changba.com/cache/photo/941190975_200_200.jpg"
req = requests.get(url,verify=False)
with open("a.jpg",\'wb\') as fw:
fw.write(req.content)
7、请求返回的数据
# print(req.json()) 返回字典,如果接口返回的不是json,那会报错
# print(req.text) #返回的是字符串
# print(req.content) #返回的是bytes的
# print(req.status_code) #返回的状态码
# print(req.cookies) #返回的cookie
# print(req.headers) #返回的header
以上是关于Python网络请求的主要内容,如果未能解决你的问题,请参考以下文章