python第三方库Requests的基本使用
Requests 是用python語言編寫,基于 urllib,采用 Apache2 Licensed 開源協(xié)議的 HTTP 庫。它比 urllib 更加方便,可以節(jié)約我們大量的工作,完全滿足 HTTP 測(cè)試需求。Requests 的哲學(xué)是以 PEP 20 的習(xí)語為中心開發(fā)的,所以它比 urllib 更加 Pythoner。
通過pip安裝
pip install requests一、最基本的get請(qǐng)求
1 import requests 2 3 req=requests.get('https://www.cnblogs.com/')#普通的get請(qǐng)求 4 print(req.text)#解析網(wǎng)頁標(biāo)簽,查找頭域head中的meta標(biāo)簽<meta charset="utf-8" /> 5 print(req.content)#出來的中文有些是亂碼,需要解碼 6 print(req.content.decode('utf-8'))#用decode解碼 1 requests.get(‘https://github.com/timeline.json’) #GET請(qǐng)求 2 requests.post(“http://httpbin.org/post”) #POST請(qǐng)求 3 requests.put(“http://httpbin.org/put”) #PUT請(qǐng)求 4 requests.delete(“http://httpbin.org/delete”) #DELETE請(qǐng)求 5 requests.head(“http://httpbin.org/get”) #HEAD請(qǐng)求 6 requests.options(“http://httpbin.org/get”) #OPTIONS請(qǐng)求
不但GET方法簡(jiǎn)單,其他方法都是統(tǒng)一的接口樣式
二、用post獲取需要用戶名密碼登陸的網(wǎng)頁?
1 import requests 2 3 postdata={ 4 'name':'estate', 5 'pass':'123456' 6 }#必須是字典類型 7 req=requests.post('http://www.iqianyue.com/mypost',data=postdata) 8 print(req.text)#進(jìn)入登陸后的頁面 9 10 yonghu=req.content#用戶登陸后的結(jié)果 11 f=open('1.html','wb')#把結(jié)果寫入1.html 12 f.write(yonghu) 13 f.close()http://www.iqianyue.com/mypost 進(jìn)入這個(gè)網(wǎng)站需要登陸,我們要定義一個(gè)字典輸入用戶名和密碼
運(yùn)行沒有報(bào)錯(cuò)可以把結(jié)果寫在一個(gè)HTML文件中
1 <html> 2 <head> 3 <title>Post Test Page</title> 4 </head> 5 6 <body> 7 <form action="" method="post"> 8 name:<input name="name" type="text" /><br> 9 passwd:<input name="pass" type="text" /><br> 10 <input name="" type="submit" value="submit" /> 11 <br /> 12 you input name is:estate<br>you input passwd is:123456</body> 13 </html>?
三、用headers針對(duì)反爬
?
1 import requests 2 3 headers={ 4 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' 5 }#請(qǐng)求頭發(fā)送請(qǐng)求,多個(gè)頭域可以直接在字典中添加 6 req=requests.get('http://maoyan.com/board',headers=headers)#傳遞實(shí)參 7 print(req.text)?1 User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36?
有些網(wǎng)頁進(jìn)去后會(huì)出現(xiàn)403禁止訪問,我們需要進(jìn)入網(wǎng)頁查找到hesders中的User-Agent并添加到字典中,讀取時(shí)要傳遞實(shí)參,運(yùn)行后并可爬取貓眼電影網(wǎng)頁信息
?
四、用cookies跳過登陸
?
1 import requests 2 3 f=open('cookies.txt','r') 4 #初始化cookies字典變量 5 cookies={} 6 #用for循環(huán)遍歷切割。按照字符;進(jìn)行切割讀取,返回列表數(shù)據(jù)然后遍歷 7 for line in f.read().split(';'): 8 #split參數(shù)設(shè)置為1,將字符串切割成兩部分 9 name,value=line.split('=',1) 10 #為字典cookies添加內(nèi)容 11 cookies[name] = value 12 url='https://www.cnblogs.com/' 13 res=requests.get(url,cookies=cookies) 14 data=res.content 15 f1=open('bokeyuan.html','wb') 16 f1.write(data) 17 f1.close() 18 f.close()先輸入用戶名和密碼登陸網(wǎng)頁后獲取網(wǎng)頁的cookies,復(fù)制粘貼到新建的文本中,創(chuàng)建一個(gè)空的cookies字典,用for循環(huán)遍歷切割。cookies中的字段按照字符 ;?進(jìn)行切割讀取成兩部分。
運(yùn)行后把結(jié)果寫到命名為bokeyuan的html文件中,進(jìn)入html文件直接點(diǎn)擊網(wǎng)頁圖標(biāo)即可進(jìn)入登陸后的頁面
?
五、代理IP
1 import requests 2 3 proxies={ 4 'HTTP':'183.129.244.17:10080' 5 } 6 req=requests.get('https://www.taobao.com/',proxies=proxies) 7 print(req.text)采集時(shí)為避免被封IP,經(jīng)常會(huì)使用代理。requests也有相應(yīng)的proxies屬性。我們可以在網(wǎng)頁上查找代理IP,在字典中輸入代理IP地址和端口,需要多個(gè)IP可以直接在字典后面添加。如果代理需要賬戶和密碼,則需這樣:
1 proxies = { 2 "http": "http://user:pass@10.10.1.10:3128/", 3 }?
六、超時(shí)設(shè)置
1 import requests 2 3 req=requests.get('https://www.taobao.com/',timeout=1) 4 print(req.text) timeout 僅對(duì)連接過程有效,與響應(yīng)體的下載無關(guān)以上為Requests庫的基礎(chǔ)操作,后續(xù)再做補(bǔ)充......
?
轉(zhuǎn)載于:https://www.cnblogs.com/Estate-47/p/9799332.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python第三方库Requests的基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缩点+染色+DFS codeforce
- 下一篇: 【校内模拟】次短路