爬虫 - requests模块
生活随笔
收集整理的這篇文章主要介紹了
爬虫 - requests模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于GET請求
1、基本請求
import requests response=requests.get('http://dig.chouti.com/') print(response.text)2、帶參數的GET請求->params
#在請求頭內將自己偽裝成瀏覽器,否則百度不會正常返回頁面內容 import requests response=requests.get('https://www.baidu.com/s?wd=python&pn=1',headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',}) print(response.text)#如果查詢關鍵詞是中文或者有其他特殊符號,則不得不進行url編碼 from urllib.parse import urlencode wd='egon老師' encode_res=urlencode({'k':wd},encoding='utf-8') print(encode_res) ## k=egon%E8%80%81%E5%B8%88 keyword=encode_res.split('=')[1] print(keyword) ## egon%E8%80%81%E5%B8%88# 然后拼接成url url='https://www.baidu.com/s?wd=%s&pn=1' %keywordresponse=requests.get(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',}) res1=response.text 自己拼接GET請求 import requests wd='egon老師' pn=1 response=requests.get('https://www.baidu.com/s',params={'wd':wd,'pn':pn},headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',}) res=response.url print(res) # https://www.baidu.com/s?wd=egon%E8%80%81%E5%B8%88&pn=1 params參數的使用3、帶參數的GET請求->headers
#通常我們在發送請求時都需要帶上請求頭,請求頭是將自身偽裝成瀏覽器的關鍵,常見的有用的請求頭如下 Host Referer #大型網站通常都會根據該參數判斷請求的來源 User-Agent #客戶端 Cookie #Cookie信息雖然包含在請求頭里,但requests模塊有單獨的參數來處理他,headers={}內就不要放它了 View Code4、帶參數的GET請求->cookies
#登錄github,然后從瀏覽器中獲取cookies,以后就可以直接拿著cookie登錄了,無需輸入用戶名密碼 #用戶名:egonlin 郵箱378533872@qq.com 密碼lhf@123import requestsCookies={ 'user_session':'wGMHFJKgDcmRIVvcA14_Wrt_3xaUyJNsBnPbYzEL6L0bHcfc', }response=requests.get('https://github.com/settings/emails',cookies=Cookies) #github對請求頭沒有什么限制,我們無需定制user-agent,對于其他網站可能還需要定制print('378533872@qq.com' in response.text) #True View Code?
基于POST請求
1、介紹
#GET請求 HTTP默認的請求方法就是GET* 沒有請求體* 數據必須在1K之內!* GET請求數據會暴露在瀏覽器的地址欄中GET請求常用的操作:1. 在瀏覽器的地址欄中直接給出URL,那么就一定是GET請求2. 點擊頁面上的超鏈接也一定是GET請求3. 提交表單時,表單默認使用GET請求,但可以設置為POST#POST請求 (1). 數據不會出現在地址欄中 (2). 數據的大小沒有上限 (3). 有請求體 (4). 請求體中如果存在中文,會使用URL編碼!#!!!requests.post()用法與requests.get()完全一致,特殊的是requests.post()有一個data參數,用來存放請求體數據2、發送post請求,模擬瀏覽器的登錄行為
#對于登錄來說,應該輸錯用戶名或密碼然后分析抓包流程,用腦子想一想,輸對了瀏覽器就跳轉了,還分析個毛線,累死你也找不到包 ''' 一 目標站點分析瀏覽器輸入https://github.com/login然后輸入錯誤的賬號密碼,抓包發現登錄行為是post提交到:https://github.com/session而且請求頭包含cookie而且請求體包含:commit:Sign inutf8:?authenticity_token:lbI8IJCwGslZS8qJPnof5e7ZkCoSoMn6jmDTsL1r/m06NLyIbw7vCrpwrFAPzHMep3Tmf/TSJVoXWrvDZaVwxQ==login:egonlinpassword:123二 流程分析先GET:https://github.com/login拿到初始cookie與authenticity_token返回POST:https://github.com/session, 帶上初始cookie,帶上請求體(authenticity_token,用戶名,密碼等)最后拿到登錄cookieps:如果密碼時密文形式,則可以先輸錯賬號,輸對密碼,然后到瀏覽器中拿到加密后的密碼,github的密碼是明文 '''import requests import re#第一次請求 r1=requests.get('https://github.com/login') r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授權) authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #從頁面中拿到CSRF TOKEN#第二次請求:帶著初始cookie和TOKEN發送POST請求給登錄頁面,帶上賬號密碼 data={'commit':'Sign in','utf8':'?','authenticity_token':authenticity_token,'login':'317828332@qq.com','password':'alex3714' } r2=requests.post('https://github.com/session',data=data,cookies=r1_cookie)login_cookie=r2.cookies.get_dict()#第三次請求:以后的登錄,拿著login_cookie就可以,比如訪問一些個人配置 r3=requests.get('https://github.com/settings/emails',cookies=login_cookie)print('317828332@qq.com' in r3.text) #True 自動登錄github(自己處理cookie信息) import requests import resession=requests.session() #第一次請求 r1=session.get('https://github.com/login') authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #從頁面中拿到CSRF TOKEN#第二次請求 data={'commit':'Sign in','utf8':'?','authenticity_token':authenticity_token,'login':'317828332@qq.com','password':'alex3714' } r2=session.post('https://github.com/session',data=data,)#第三次請求 r3=session.get('https://github.com/settings/emails')print('317828332@qq.com' in r3.text) #True requests.session()自動幫我們保存cookie信息3、補充
requests.post(url='xxxxxxxx',data={'xxx':'yyy'}) #沒有指定請求頭,#默認的請求頭:application/x-www-form-urlencoed#如果我們自定義請求頭是application/json,并且用data傳值, 則服務端取不到值 requests.post(url='',data={'':1,},headers={'content-type':'application/json'})requests.post(url='',json={'':1,},) #默認的請求頭:application/json View Code?
響應Response
1、response屬性
import requests respone=requests.get('http://www.jianshu.com') # respone屬性 print(respone.text) # 字符串 print(respone.content) # 二進制print(respone.status_code) # 狀態碼 print(respone.headers) # 響應頭 print(respone.cookies) print(respone.cookies.get_dict()) # 字典形式的COOKIES print(respone.cookies.items()) # 列表套元祖形式的COOKIESprint(respone.url) # 請求的url print(respone.history) #跳轉前的responseprint(respone.encoding) # 頁面的編碼 print(response.iter_content()) # 響應的二進制數據在一個生成器中 View Code2、編碼問題
#編碼問題 import requests response=requests.get('http://www.autohome.com/news') # response.encoding='gbk' #汽車之家網站返回的頁面內容為gb2312編碼的,而requests的默認編碼為ISO-8859-1,如果不設置成gbk則中文亂碼 print(response.text) View Code3、獲取二進制數據
import requestsresponse=requests.get('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509868306530&di=712e4ef3ab258b36e9f4b48e85a81c9d&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F11385343fbf2b211e1fb58a1c08065380dd78e0c.jpg')with open('a.jpg','wb') as f:f.write(response.content) View Code #stream參數:一點一點的取,比如下載視頻時,如果視頻100G,用response.content然后一下子寫到文件中是不合理的import requestsresponse=requests.get('https://gss3.baidu.com/6LZ0ej3k1Qd3ote6lo7D0j9wehsv/tieba-smallvideo-transcode/1767502_56ec685f9c7ec542eeaf6eac93a65dc7_6fe25cd1347c_3.mp4',stream=True)with open('b.mp4','wb') as f:for line in response.iter_content():f.write(line) 獲取二進制流4、獲取json數據
#解析json import requests response=requests.get('http://httpbin.org/get')import json res1=json.loads(response.text) #太麻煩 res2=response.json() #直接獲取json數據print(res1 == res2) #True View Code5、Redirection and History
import requests import re#第一次請求 r1=requests.get('https://github.com/login') r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授權) authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #從頁面中拿到CSRF TOKEN#第二次請求:帶著初始cookie和TOKEN發送POST請求給登錄頁面,帶上賬號密碼 data={'commit':'Sign in','utf8':'?','authenticity_token':authenticity_token,'login':'317828332@qq.com','password':'alex3714' }#測試一:沒有指定allow_redirects=False,則響應頭中出現Location就跳轉到新頁面,r2代表新頁面的response r2=requests.post('https://github.com/session',data=data,cookies=r1_cookie)print(r2.status_code) #200 print(r2.url) #看到的是跳轉后的頁面 print(r2.history) #看到的是跳轉前的response print(r2.history[0].text) #看到的是跳轉前的response.text#測試二:指定allow_redirects=False,則響應頭中即便出現Location也不會跳轉到新頁面,r2代表的仍然是老頁面的response r2=requests.post('https://github.com/session',data=data,cookies=r1_cookie,allow_redirects=False)print(r2.status_code) #302 print(r2.url) #看到的是跳轉前的頁面https://github.com/session print(r2.history) #[] requests中的參數:allow_redirects?
高級用法
1、SSL Cert Verification?? 證書驗證
#證書驗證(大部分網站都是https) import requests respone=requests.get('https://www.12306.cn') #如果是ssl請求,首先檢查證書是否合法,不合法則報錯,程序終端#改進1:去掉報錯,但是會報警告 import requests respone=requests.get('https://www.12306.cn',verify=False) #不驗證證書,報警告,返回200 print(respone.status_code)#改進2:去掉報錯,并且去掉警報信息 import requests from requests.packages import urllib3 urllib3.disable_warnings() #關閉警告 respone=requests.get('https://www.12306.cn',verify=False) print(respone.status_code)#改進3:加上證書 #很多網站都是https,但是不用證書也可以訪問,大多數情況都是可以攜帶也可以不攜帶證書 #知乎\百度等都是可帶可不帶 #有硬性要求的,則必須帶,比如對于定向的用戶,拿到證書后才有權限訪問某個特定網站 import requests respone=requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key')) print(respone.status_code) View Code2、使用代理
#官網鏈接: http://docs.python-requests.org/en/master/user/advanced/#proxies#代理設置:先發送請求給代理,然后由代理幫忙發送(封ip是常見的事情) import requests proxies={'http':'http://egon:123@localhost:9743',#帶用戶名密碼的代理,@符號前是用戶名與密碼,后面是代理IP與代理端口'http':'http://localhost:9743','https':'https://localhost:9743', } respone=requests.get('https://www.12306.cn',proxies=proxies)print(respone.status_code)#支持socks代理,安裝:pip install requests[socks] import requests proxies = {'http': 'socks5://user:pass@host:port','https': 'socks5://user:pass@host:port' } respone=requests.get('https://www.12306.cn',proxies=proxies)print(respone.status_code) View Code3、超時時間
超時設置 #兩種超時:float or tuple #timeout=0.1 #代表接收數據的超時時間 #timeout=(0.1,0.2)#0.1代表鏈接超時 0.2代表接收數據的超時時間import requests respone=requests.get('https://www.baidu.com',timeout=0.0001) View Code4、認證設置
#官網鏈接:http://docs.python-requests.org/en/master/user/authentication/#認證設置:登陸網站是,彈出一個框,要求你輸入用戶名密碼(與alter很類似),此時是無法獲取html的 # 但本質原理是拼接成請求頭發送 # r.headers['Authorization'] = _basic_auth_str(self.username, self.password) # 一般的網站都不用默認的加密方式,都是自己寫 # 那么我們就需要按照網站的加密方式,自己寫一個類似于_basic_auth_str的方法 # 得到加密字符串后添加到請求頭 # r.headers['Authorization'] =func('.....')#看一看默認的加密方式吧,通常網站都不會用默認的加密設置 import requests from requests.auth import HTTPBasicAuth r=requests.get('xxx',auth=HTTPBasicAuth('user','password')) print(r.status_code)#HTTPBasicAuth可以簡寫為如下格式 import requests r=requests.get('xxx',auth=('user','password')) print(r.status_code) View Code轉載于:https://www.cnblogs.com/chengdajing/articles/9239718.html
總結
以上是生活随笔為你收集整理的爬虫 - requests模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @echo 与 echo的区别
- 下一篇: 2018.06.28 与或(线段树)