Python requests詳解

  •  Requests

參考文檔http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlhtml

 1 import requests  2 response = requests.get('http://www.baidu.com')  3 
 4 #文本形式的網頁源碼
 5 print(response.text)  6 
 7 #二進制流形式打印
 8 print(response.content)  9 
10 #返回JSON格式,可能拋出異常
11 print(response.json) 12 
13 #狀態碼
14 print(response.status_code) 15 
16 #請求url
17 print(response.url) 18 
19 #頭信息
20 print(response.headers) 21 
22 #cookie信息
23 print(response.cookies)
View Code
  • 傳遞參數

   

1 import requests 2 
3 payload = {'key1': 'value1', 'key2': 'value2', 'key3': None} 4 r = requests.get('http://httpbin.org/get', params=payload) 5 print(r.url)
View Code

 

  • 請求方式

 

1 requests.get('http://httpbin.org/get') 2 requests.post('http://httpbin.org/post') 3 requests.put('http://httpbin.org/put') 4 requests.delete('http://httpbin.org/delete') 5 requests.head('http://httpbin.org/get') 6 requests.options('http://httpbin.org/get')
View Code
  • post訪問
1 import requests 2 payload = {'key1': 'value1', 'key2': 'value2'} 3 r = requests.post("http://httpbin.org/post", data=payload) 4 print(r.text)
View Code
  • 傳遞文件
1 import requests 2 url = 'http://httpbin.org/post'
3 files = {'file': open('wyl.xls', 'rb')} 4 r = requests.post(url, files=files)
View Code
  • 傳遞字符串
1 import requests 2 import json 3 url = 'https://xxxxxxxx'
4 payload = {'some': 'data'} 5 r = requests.post(url, data=json.dumps(payload)) 6 #或者
7 r = requests.post(url, json=payload)
View Code
  • 超時設置

 1 requests.get('http://xxxxx.com', timeout=1) python

  • 代理
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080', } requests.get('http://xxxx.com', proxies=proxies)
  • 重定向與請求歷史
import requests s=requests.get('http://github.com') print(s.url) print(s.status_code) r=requests.get('http://github.com', allow_redirects=False) print(r.url) print(r.history) r=requests.head('http://github.com'); print(r.url) print(r.status_code) r=requests.head('http://github.com', allow_redirects=True) print(r.url) print(r.history)
  • 異常處理
#全部Requests顯式拋出的異常都繼承自 requests.exceptions.RequestException

import requests from requests.exceptions import ReadTimeout,HTTPError,RequestException try: response = requests.get('http://www.baidu.com',timeout=0.5) print(response.status_code) except ReadTimeout: print('timeout') except HTTPError: print('httperror') except RequestException: print('reqerror')

HTTPError:若是 HTTP 請求返回了不成功的狀態碼git

Timeout:請求超時github

ConnectionError:遇到網絡問題(如:DNS 查詢失敗、拒絕鏈接等)json

TooManyRedirects:若請求超過了設定的最大重定向次數cookie

RequestException:全部的requerst 異常網絡

  • requests登錄的幾種方法

 

#經過帳號和密碼登錄
 loginurl='https://xxxxx.com/check' formData={'username':'*****', 'password':'*****'} headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/52.0'} res=req.post(loginurl,data=formData,headers=headers)

 

#經過COOKIES
 raw_cookies="k1=v1; k2=v2; k3=v3; 
cookies={} for line in raw_cookies.split(';'): key,value=line.split('=',1) cookies[key]=value loginurl='http://xxxxxx.com' res=req.post(loginurl,cookies=cookies) print res.content "訪問其它的頁面" logi1="http://xxxxx.htm"  
print req.post(logi1,cookies=cookies).content

 

 

 

 

#經過session

import requests as req s=req.Session() param={'username':'****', 'password':'***'} url='https://xxxxxx' r=s.post(url,data=param,verify=False)  #登陸獲取登陸後的session 
print r.content print s.get('http://xxxxxxx',verify=False).content  #經過session訪問其它url
相關文章
相關標籤/搜索