requests 进阶用法学习(文件上传cookies设置代理设置)
Posted angle6-liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了requests 进阶用法学习(文件上传cookies设置代理设置)相关的知识,希望对你有一定的参考价值。
一、文件上传
1、模拟网站提交文件
提交此图片,图片名称:timg.jpg
import requests files={ ‘file‘:open(‘timg.jpg‘,‘rb‘) } response=requests.post(‘http://httpbin.org/post‘,files=files) print(response.text)
{ "args": {}, "data": "", "files": { "file": "data:application/octet-stream;base64..." }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "27595", "Content-Type": "multipart/form-data; boundary=bfa4e305bf1a07aeff0b161b6b6acd98", "Host": "httpbin.org", "User-Agent": "python-requests/2.20.1" }, "json": null, "origin": "119.123.198.80", "url": "http://httpbin.org/post" }
二、cookies (requests获取和设置cookies只需要一步)
1、什么是cookie
使用r.cookies获取cookie(key,value)
import requests r=requests.get("http://www.hao123.com") print(r.cookies) for key,value in r.cookies.items(): print(key+‘=‘+value)
结果显示cookies
<RequestsCookieJar[<Cookie BAIDUID=D08FC3C77CD769084E0F74D7A4B1415D:FG=1 for .hao123.com/>, <Cookie hz=0 for .www.hao123.com/>, <Cookie ft=1 for www.hao123.com/>, <Cookie v_pg=normal for www.hao123.com/>]> BAIDUID=D08FC3C77CD769084E0F74D7A4B1415D:FG=1 hz=0 ft=1 v_pg=normal
2、cookie的作用:维持登录状态
如何使用cookie:在header中包含登录信息的cookies即可维持回话登录.
import requests headers={ ‘user-agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.110 Safari/537.36‘, ‘cookie‘:‘_zap=90f75832-31eb-4d2d-b35f-9ccaf9fb751d; d_c0="ABDix0gVng6PTouChpyAoAQ3G7to4zgcmSA=|1543906948"; capsion_ticket="2|1:0|10:1543906948|14:capsion_ticket|44:YjVkZTgwZGJhMDkxNDk1Nzk0ZWQzY2QxNmY3Y2I4ZTA=|5eb3fc941322cc1a46641b7a01cf3e1d4bd2af3e957c9034e11bf5ce866beef4"; q_c1=cf87a31041fd439286ff979925591375|1543906958000|1543906958000; r_cap_id="Y2Y5ODA2MzcwNjc2NGY2NTg3YzBmMjY0ODgzNzVjZTY=|1543906958|90e763acce4047123b3af3f2704353c655212d04"; cap_id="YjY2MmE1ZDZkNWRmNDY3NWI0ZTE0NDM5OWQ4ZGIyN2Y=|1543906958|cfd125126f4cb0595540b10a1345fe83ed78b54c"; l_cap_id="YzhkZmY1ZGE4MTI5NGFkY2I2OWEyNzEzYmUzYzc1NTk=|1543906958|cfb980d103d69a8a16e49fbc84d6c9eb9f259656"; z_c0="2|1:0|10:1543907047|4:z_c0|92:Mi4xdVdsa0RRQUFBQUFBRU9MSFNCV2VEaVlBQUFCZ0FsVk41M1R6WEFEcGZiR3REaExaUzU2QVI1RUs1cjFHSWhlTGRn|8cffbfc2e83f1bbf0b257fe911a35f32e1411981c36631e680d48b52cbb4acdf"; __gads=ID=ca4c27d035179b38:T=1543907482:S=ALNI_MaXKoFRpK-yi_viwOXIAxef01d82A; tst=h; _xsrf=90cd6bdbf65ff7b9f06741d408360edf; _xsrf=nMfSyfddjAeWdq5Fjx3UFFJClFbYdPO4; __utma=155987696.97682671.1543926682.1543926682.1543926682.1; __utmz=155987696.1543926682.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); OUTFOX_SEARCH_USER_ID_NCOO=1988278338.5008798; tgw_l7_route=29b95235203ffc15742abb84032d7e75‘ } response=requests.get(‘http://www.zhihu.com‘,headers=headers) print(response.status_code)
#请用自己的cookie
三、会话维持session
什么叫做会话维持:登陆成功后,在使用request希望能访问之后的页面,不需要重新输入账户密码的方式叫做会话维持。
requests请求多个网页相当于启动了多个浏览器,这样就要多次设计cookies
#Session回话维持 import requests s=requests.Session() r1=s.get(‘http://httpbin.org/cookies/set/number/123456789‘) r2=s.get(‘http://httpbin.org/cookies‘) print(r1.text) print(r2.text)
{ "cookies": { "number": "123456789" } } { "cookies": { "number": "123456789" } }
不适用Session回话维持,两次访问的结果如下
import requests s=requests r1=s.get(‘http://httpbin.org/cookies/set/number/123456789‘) r2=s.get(‘http://httpbin.org/cookies‘) print(r1.text) print(r2.text)
{ "cookies": { "number": "123456789" } } { "cookies": {} }
利用 Session ,可以做到模拟同一个会话而不用担心 Cookies 的问题。 它通常用于模拟登录
成功之后再进行下一步的操作 。
四、SSL证书验证
impo:r:t requests 于ram requests.packages import urllib3 urllib3 .disable_warnings() response = requests.get (’ https : //州 .12306 . cn ’, verify= False)
五、代理设置proxies
(1)代理格式1:
proxies={ ‘http‘:‘http://10.10.1.10:3128‘, ‘https‘:‘http://10.10.1.10:1080‘ } res=requests.get(‘http://www.taobao.com‘,proxies=proxies)
(2)代理格式2:代理需要使用 HTTP Basic Auth ,可以使用类似 http://user:[email protected]:port 这样的语法来设置代理 。
proxies = { "http””http://user:[email protected] .10:3128/”, } requests . get (咱ttps : I lwww. taobao. com”, proxies=proxies)
六、超时设置(timeout=)
超时一秒就开始报超时
res=requests.get(‘http://www.taobao.com‘,timeout=1)
七、身份认证
1、格式一:
import requests from requests.auth import HTTPBasicAuth r=requests.get(‘http://ww.baidu.com‘,auth=HTTPBasicAuth(‘username‘,‘password‘))
2、格式二
import requests r=requests.get(‘http://ww.baidu.com‘,auth=(‘username‘,‘password‘))
八、构造请求结构--Prepared Request
from requests import Request,Session url=‘http://httpbin.org/post‘ data={ ‘name‘:‘big pig‘ } headers={ ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36‘ } s=Session() #构造请求体 req=Request(‘POST‘,url,data=data,headers=headers) prepped=s.prepare_request(req) r=s.send(prepped) print(r.text)
这里我们引入了Request,然后用url、data和headers参数构造了一个Requestd对象,这时需要再调用Session的prepare_request( )方法将其转换为Prepare Request对象,然后调用send 方法发送,其运行结果:
{ "args": {}, "data": "", "files": {}, "form": { "name": "big pig" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "12", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" }, "json": null, "origin": "117.136.79.133", "url": "http://httpbin.org/post" }