requests庫
雖然Python的標(biāo)準(zhǔn)庫中 urllib模塊已經(jīng)包含了平常我們使用的大多數(shù)功能榄融,但是它的 API 使用起來讓人感覺不太好缀蹄,而 Requests宣傳是 “HTTP for Humans”灼芭,說明使用更簡潔方便伙判。
安裝和文檔地址:
利用pip可以非常方便的安裝:
pip install requests
中文文檔:http://docs.python-requests.org/zh_CN/latest/index.html
github地址:https://github.com/requests/requests
Pycharm激活碼教程使用更多解釋請見:http://vrg123.com
發(fā)送GET請求:
最簡單的發(fā)送get請求就是通過requests.get來調(diào)用:
response = requests.get("http://www.baidu.com/")
添加headers和查詢參數(shù):
如果想添加 headers关噪,可以傳入headers參數(shù)來增加請求頭中的headers信息。如果要將參數(shù)放在url中傳遞泣洞,可以利用 params 參數(shù)。相關(guān)示例代碼如下:
importrequests kw = {'wd':'中國'} headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}# params 接收一個字典或者字符串的查詢參數(shù)默色,字典類型自動轉(zhuǎn)換為url編碼球凰,不需要urlencode()response = requests.get("http://www.baidu.com/s", params = kw, headers = headers)# 查看響應(yīng)內(nèi)容,response.text 返回的是Unicode格式的數(shù)據(jù)print(response.text)# 查看響應(yīng)內(nèi)容腿宰,response.content返回的字節(jié)流數(shù)據(jù)print(response.content)# 查看完整url地址print(response.url)# 查看響應(yīng)頭部字符編碼print(response.encoding)# 查看響應(yīng)碼print(response.status_code)
發(fā)送POST請求:
最基本的POST請求可以使用post方法:
response = requests.post("http://www.baidu.com/",data=data)
傳入data數(shù)據(jù):
這時候就不要再使用urlencode進(jìn)行編碼了呕诉,直接傳入一個字典進(jìn)去就可以了。比如請求拉勾網(wǎng)的數(shù)據(jù)的代碼:
importrequests url ="https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false&isSchoolJob=0"headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36','Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='} data = {'first':'true','pn':1,'kd':'python'} resp = requests.post(url,headers=headers,data=data)# 如果是json數(shù)據(jù)酗失,直接可以調(diào)用json方法print(resp.json())
使用代理:
使用requests添加代理也非常簡單义钉,只要在請求的方法中(比如get或者post)傳遞proxies參數(shù)就可以了。示例代碼如下:
importrequestsurl ="http://httpbin.org/get"headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',}proxy = {'http':'171.14.209.180:27829'}resp = requests.get(url,headers=headers,proxies=proxy)withopen('xx.html','w',encoding='utf-8')asfp:? ? fp.write(resp.text)
cookie:
如果在一個響應(yīng)中包含了cookie规肴,那么可以利用cookies屬性拿到這個返回的cookie值:
importrequestsurl ="http://www.renren.com/PLogin.do"data = {"email":"970138074@qq.com",'password':"pythonspider"}resp = requests.get('http://www.baidu.com/')print(resp.cookies)print(resp.cookies.get_dict())
session:
之前使用urllib庫捶闸,是可以使用opener發(fā)送多個請求,多個請求之間是可以共享cookie的拖刃。那么如果使用requests删壮,也要達(dá)到共享cookie的目的,那么可以使用requests庫給我們提供的session對象兑牡。注意央碟,這里的session不是web開發(fā)中的那個session,這個地方只是一個會話的對象而已均函。還是以登錄人人網(wǎng)為例亿虽,使用requests來實現(xiàn)。示例代碼如下:
importrequestsurl ="http://www.renren.com/PLogin.do"data = {"email":"970138074@qq.com",'password':"pythonspider"}headers = {'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}# 登錄session = requests.session()session.post(url,data=data,headers=headers)# 訪問大鵬個人中心resp = session.get('http://www.renren.com/880151247/profile')print(resp.text)
處理不信任的SSL證書:
對于那些已經(jīng)被信任的SSL整數(shù)的網(wǎng)站苞也,比如https://www.baidu.com/洛勉,那么使用requests直接就可以正常的返回響應(yīng)。示例代碼如下:
resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)print(resp.content.decode('utf-8'))