1. 程式人生 > >requests 進階用法學習(檔案上傳、cookies設定、代理設定)

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"
  }
}
執行結果(網站的cookie資訊一致)

不適用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)
不適用Session程式碼
{
  "cookies": {
    "number": "123456789"
  }
}

{
  "cookies": {}
}
不適用Session的cookie

   利用 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"
}
執行結果