為什么要學習requests
,而不是urllib
壹罚?
- requests的底層實現(xiàn)就是urllib
- requests在python2 和python3中通用税课,方法完全一樣
- requests簡單易用
- Requests能夠自動幫助我們解壓(gzip壓縮的等)網頁內容
- 作用:發(fā)送網絡請求拨匆,返回響應數(shù)據(jù)
- 中文文檔 API: http://docs.python-requests.org/zh_CN/latest/index.html
安裝方式
利用 pip 安裝 或者利用 easy_install 都可以完成安裝:
- pip install requests
- easy_install requests
- 安裝第三方模塊
- pip install 模塊名
- 下載源碼解碼,進入解壓后的目錄
python setup.py install
-
***.whl
安裝方法pip install ***.whl
基本GET請求
- response = requests.get("http://www.baidu.com/")
也可以這么寫 - response = requests.request("get", "http://www.baidu.com/")
添加 headers 和 查詢參數(shù)
如果想添加 headers账忘,可以傳入headers參數(shù)來增加請求頭中的headers信息。如果要將參數(shù)放在url中傳遞熙宇,可以利用 params 參數(shù)鳖擒。
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ù),字典類型自動轉換為url編碼烫止,不需要urlencode() response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)
response的常用方法:
- response.text
- respones.content
- response.status_code
- response.encoding
- response.request.headers
- response.headers
- response.url
response.text 和response.content的區(qū)別
- response.text
類型:str
解碼類型: 根據(jù)HTTP 頭部對響應的編碼作出有根據(jù)的推測蒋荚,推測的文本編碼
如何修改編碼方式:response.encoding=”gbk” - response.content
類型:bytes
解碼類型: 沒有指定
如何修改編碼方式:response.content.deocde(“utf8”) - 使用response.text 時,Requests 會基于 HTTP 響應的文本編碼自動解碼響應內容馆蠕,大多數(shù) Unicode 字符集都能被無縫地解碼期升。
- 使用response.content 時,返回的是服務器響應數(shù)據(jù)的原始二進制字節(jié)流互躬,可以用來保存圖片等二進制文件播赁。
- 更推薦使用response.content.deocde()的方式獲取響應的html頁面
為什么請求需要帶上header?
- 模擬瀏覽器吼渡,欺騙服務器容为,獲取和瀏覽器一致的內容
header的形式:字典
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"}
用法: requests.get(url,headers=headers)
發(fā)送帶參數(shù)的請求
什么叫做請求參數(shù)
列1: http://www.webkaka.com/tutorial/server/2015/021013/
例2: https://www.baidu.com/s?wd=python&c=b
參數(shù)的形式:字典
kw = {'wd':'長城'}
用法:requests.get(url,params=kw)
基本POST請求
哪些地方我們會用到POST請求:
- 登錄注冊( POST 比 GET 更安全)
- 需要傳輸大文本內容的時候( POST 請求對數(shù)據(jù)長度沒有要求)
- 所以同樣的,我們的爬蟲也需要在這兩個地方會去模擬瀏覽器發(fā)送post請求
最基本post方法
- response = requests.post("http://www.baidu.com/", data = data)
傳入data數(shù)據(jù)
對于 POST 請求來說诞吱,我們一般需要為它增加一些參數(shù)舟奠。那么最基本的傳參方法可以利用 data 這個參數(shù)
import requests formdata = { "type":"AUTO", "i":"i love python", "doctype":"json", "xmlVersion":"1.8", "keyfrom":"fanyi.web", "ue":"UTF-8", "action":"FY_BY_ENTER", "typoResult":"true" } url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null" headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"} response = requests.post(url, data = formdata, headers = headers) print (response.text) # 如果是json文件可以直接顯示 print (response.json())
尋找登錄的post地址
- 在form表單中尋找action對應的URL地址
post的數(shù)據(jù)是input標簽中name的值作為鍵,真正的用戶名密碼作為值的字典房维,post的URL地址就是
action對應的URL地址-
抓包沼瘫,尋找登錄的url地址
- 勾選perserve log按鈕,防止頁面跳轉找不到URL
- 尋找post數(shù)據(jù)咙俩,確定參數(shù)
- 參數(shù)不會變耿戚,直接用,比如密碼不是動態(tài)加密的時候
- 參數(shù)會變
- 參數(shù)在當前的響應中
- 通過js生成
定位想要的js
- 選擇會觸發(fā)js事件的按鈕阿趁,點擊event listener 找到js的位置
- 通過chorme中的search all file來搜索url中關鍵字