Requests 是用Python語言編寫,基于 urllib,采用 Apache2 Licensed 開源協(xié)議的 HTTP 庫(kù)。它比 urllib 更加方便,可以節(jié)約我們大量的工作,完全滿足 HTTP 測(cè)試需求。Requests 的哲學(xué)是以 PEP 20 的習(xí)語為中心開發(fā)的,所以它比 urllib 更加 Pythoner。
其功能有以下:
- 國(guó)際化域名和 URLs
- Keep-Alive & 連接池
- 持久的 Cookie 會(huì)話
- 類瀏覽器式的 SSL 加密認(rèn)證
- 基本/摘要式的身份認(rèn)證
- 優(yōu)雅的鍵/值 Cookies
- 自動(dòng)解壓
- Unicode 編碼的響應(yīng)體
- 多段文件上傳
- 連接超時(shí)
- 支持 .netrc
- 適用于 Python 2.6—3.4
- 線程安全
官方文檔講的非常好,鏈接如下:
英文文檔:http://www.python-requests.org/en/master/
中文文檔:http://docs.python-requests.org/zh_CN/latest/index.html
還有這篇博文也不錯(cuò):http://blog.csdn.net/shanzhizi/article/details/50903748
前段時(shí)間在寫一個(gè)小工具的時(shí)候我也使用過另一個(gè)模塊urllib,個(gè)人覺得跟requests比起來確實(shí)繁雜了許多。
小工具要實(shí)現(xiàn)的功能是登錄公司業(yè)務(wù)平臺(tái),然后請(qǐng)求設(shè)備上傳到平臺(tái)的原始報(bào)文數(shù)據(jù),并保存到本地。
程序要做的事其實(shí)就是調(diào)兩次接口:
- 登錄平臺(tái)(post)
- 請(qǐng)求接口獲取原始報(bào)文數(shù)據(jù)(get)
(保存請(qǐng)求數(shù)據(jù)跟requests沒啥關(guān)系就忽略了)
下面貼代碼做個(gè)比較:
urllib版
from urllib.parse import urlparse
import urllib
import urllib.request
import urllib.response
import http.cookiejar
import configparser
conf = configparser.ConfigParser()
conf.read("new_gps.conf")
# 登錄的主頁(yè)面
hosturl = conf.get("login", "hosturl")
# post數(shù)據(jù)接收和處理的頁(yè)面(我們要向這個(gè)頁(yè)面發(fā)送我們構(gòu)造的Post數(shù)據(jù))
posturl = conf.get("login", "posturl")
# 設(shè)置一個(gè)cookie處理器,它負(fù)責(zé)從服務(wù)器下載cookie到本地,并且在發(fā)送請(qǐng)求時(shí)帶上本地的cookie
cj = http.cookiejar.LWPCookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
# 打開登錄主頁(yè)面(他的目的是從頁(yè)面下載cookie,這樣我們?cè)谠偎蚿ost數(shù)據(jù)時(shí)就有cookie了,否則發(fā)送不成功)
h = urllib.request.urlopen(hosturl)
# 構(gòu)造header,一般header至少要包含一下兩項(xiàng)。
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
# 構(gòu)造Post數(shù)據(jù).
postData = {'email': conf.get("login", "email"),
'password': conf.get("login", "password"),
'url': conf.get("login", "url")
}
# 需要給Post數(shù)據(jù)編碼
postData = urllib.parse.urlencode(postData).encode('utf-8')
# 通過urllib2提供的request方法來向指定Url發(fā)送我們構(gòu)造的數(shù)據(jù),并完成登錄過程
request = urllib.request.Request(posturl, postData, headers)
response = urllib.request.urlopen(request)
# 請(qǐng)求接口獲取原始報(bào)文數(shù)據(jù)
download_url = conf.get("download", "url")
request = urllib.request.Request(download_url)
response = urllib.request.urlopen(request)
text = response.read()
嚯~就請(qǐng)求倆接口洋洋灑灑這么多行!!
(不過也不排除有更簡(jiǎn)單的寫法,畢竟之前是從網(wǎng)上copy過來的代碼改改了改,嘿嘿)
那么來看看用requests寫是什么樣的。
requests版
import requests
import json
import configparser
conf = configparser.ConfigParser()
conf.read("new_gps.conf")
url = conf.get("login", "posturl")
download_url = conf.get("download", "url")
postData = {'email': conf.get("login", "email"),
'password': conf.get("login", "password"),
'url': conf.get("login", "url")
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
s = requests.Session()
# 登錄
request = s.post(url, data=login, headers=headers, verify=False)
# 請(qǐng)求
request = s.get(download_url)
text = request.text
哇偶,看起來清爽了很多有木有?果然是requests君更人性化更優(yōu)雅呀~
兩者比較一下,會(huì)發(fā)現(xiàn)處理過程基本是一致的,但urllib比requests多了些什么呢?
- 有木有發(fā)現(xiàn)urllib在import的時(shí)候就已經(jīng)比requests多好多啊
- 在保存cookie方面,urllib君看起來真是挺費(fèi)事,但requests的session就非常方便啦
會(huì)話對(duì)象requests.Session能夠跨請(qǐng)求地保持某些參數(shù),比如cookies,即在同一個(gè)Session實(shí)例發(fā)出的所有請(qǐng)求都保持同一個(gè)cookies,而requests模塊每次會(huì)自動(dòng)處理cookies,這樣就很方便地處理登錄時(shí)的cookies問題。在cookies的處理上會(huì)話對(duì)象一句話可以頂過好幾句urllib模塊下的操作。