python第三方库Requests的基本使用
Requests 是用python語言編寫,基于 urllib,采用 Apache2 Licensed 開源協議的 HTTP 庫。它比 urllib 更加方便,可以節約我們大量的工作,完全滿足 HTTP 測試需求。Requests 的哲學是以 PEP 20 的習語為中心開發的,所以它比 urllib 更加 Pythoner。
通過pip安裝
pip install requests一、最基本的get請求
1 import requests 2 3 req=requests.get('https://www.cnblogs.com/')#普通的get請求 4 print(req.text)#解析網頁標簽,查找頭域head中的meta標簽<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請求 2 requests.post(“http://httpbin.org/post”) #POST請求 3 requests.put(“http://httpbin.org/put”) #PUT請求 4 requests.delete(“http://httpbin.org/delete”) #DELETE請求 5 requests.head(“http://httpbin.org/get”) #HEAD請求 6 requests.options(“http://httpbin.org/get”) #OPTIONS請求
不但GET方法簡單,其他方法都是統一的接口樣式
二、用post獲取需要用戶名密碼登陸的網頁?
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)#進入登陸后的頁面 9 10 yonghu=req.content#用戶登陸后的結果 11 f=open('1.html','wb')#把結果寫入1.html 12 f.write(yonghu) 13 f.close()http://www.iqianyue.com/mypost 進入這個網站需要登陸,我們要定義一個字典輸入用戶名和密碼
運行沒有報錯可以把結果寫在一個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針對反爬
?
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 }#請求頭發送請求,多個頭域可以直接在字典中添加 6 req=requests.get('http://maoyan.com/board',headers=headers)#傳遞實參 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?
有些網頁進去后會出現403禁止訪問,我們需要進入網頁查找到hesders中的User-Agent并添加到字典中,讀取時要傳遞實參,運行后并可爬取貓眼電影網頁信息
?
四、用cookies跳過登陸
?
1 import requests 2 3 f=open('cookies.txt','r') 4 #初始化cookies字典變量 5 cookies={} 6 #用for循環遍歷切割。按照字符;進行切割讀取,返回列表數據然后遍歷 7 for line in f.read().split(';'): 8 #split參數設置為1,將字符串切割成兩部分 9 name,value=line.split('=',1) 10 #為字典cookies添加內容 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()先輸入用戶名和密碼登陸網頁后獲取網頁的cookies,復制粘貼到新建的文本中,創建一個空的cookies字典,用for循環遍歷切割。cookies中的字段按照字符 ;?進行切割讀取成兩部分。
運行后把結果寫到命名為bokeyuan的html文件中,進入html文件直接點擊網頁圖標即可進入登陸后的頁面
?
五、代理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)采集時為避免被封IP,經常會使用代理。requests也有相應的proxies屬性。我們可以在網頁上查找代理IP,在字典中輸入代理IP地址和端口,需要多個IP可以直接在字典后面添加。如果代理需要賬戶和密碼,則需這樣:
1 proxies = { 2 "http": "http://user:pass@10.10.1.10:3128/", 3 }?
六、超時設置
1 import requests 2 3 req=requests.get('https://www.taobao.com/',timeout=1) 4 print(req.text) timeout 僅對連接過程有效,與響應體的下載無關以上為Requests庫的基礎操作,后續再做補充......
?
轉載于:https://www.cnblogs.com/Estate-47/p/9799332.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python第三方库Requests的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缩点+染色+DFS codeforce
- 下一篇: 【校内模拟】次短路