python爬蟲requests的使用


1 發送get請求獲取頁面

 1 import requests  2 
 3 # 1 要爬取的頁面地址
 4 url = 'http://www.baidu.com'
 5 # 2 發送get請求 拿到響應
 6 response = requests.get(url=url)  7 # 3 獲取響應內容文本 兩種方法
 8 html1 = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
 9 print(html1) 10 
11 response.encoding='utf8'
12 html2 = response.text # 用response.text 會自動選擇一種方式解碼 有時候會亂碼,要提前設置response.encoding
13 print(html2)

 

 

2 發送post請求獲取頁面

 1 import requests  2 
 3 # 1 要爬取的頁面地址
 4 url = 'http://www.baidu.com'
 5 # 2 發送get請求 拿到響應
 6 response = requests.post(url=url)  7 # 3 獲取響應內容文本 兩種方法
 8 html1 = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
 9 print(html1) 10 
11 response.encoding='utf8'
12 html2 = response.text # 用response.text 會自動選擇一種方式解碼 有時候會亂碼,要提前設置response.encoding
13 print(html2)

 

3 偽裝瀏覽器,攜帶報頭

 1 import requests  2 
 3 # 偽裝我們的報文頭,加上Use-Agent 偽裝成瀏覽器
 4 headers = {  5     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',  6     # 如果要帶着cookie 可以傳入cookie,也可以放在報文頭當中
 7     #'Cookie':'這里放入cookie'
 8 }  9 # 1 要爬取的頁面地址
10 url = 'http://www.baidu.com'
11 # 2 發送get請求 拿到響應
12 response = requests.get(url=url,headers=headers) 13 # 3 獲取響應內容文本 兩種方法
14 html = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
15 print(html)

 

4 攜帶數據 (比如 發送請求去登陸)

 

 1 import requests  2 
 3 # 如果偽裝登錄,可以傳送一個字典類型數據
 4 data = {  5 '''這里放入需要的key:value'''
 6 }  7 # 1 要爬取的頁面地址
 8 url = 'http://www.baidu.com'
 9 # 2 發送get請求 拿到響應 
10 # get請求用params 相當於在url后面拼接key=value&key=value
11 response = requests.get(url=url,params=data) 12 # post用data傳入參數 攜帶post的數據
13 response = requests.post(url=url,data=data) 14 # 3 獲取響應內容文本 兩種方法
15 html = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
16 print(html) 

 

 

 

 

5 代理

import requests # 將代理的服務器放入這里,key為協議類型 value為代理的ip和端口 # 發送https或者http請求會根據不同代理ip選擇 為我們發送請求
proxies = { 'http':'http://127.0.0.1:80', 'https':'https://127.0.0.1:80' } # 1 要爬取的頁面地址
url = 'http://www.baidu.com'
# 2 發送get請求 拿到響應
response = requests.get(url=url,proxies=proxies) # 3 獲取響應內容文本 兩種方法
html = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
print(html)

 

6 攜帶cookie

 1 import requests  2 
 3 # 如果要帶着cookie字典 可以傳入cookie,也可以放在報文頭當中
 4 cookies = {  5     #'key':'value',
 6 }  7 
 8 # 或者將cookie放在報文頭當中
 9 headers = { 10     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 11     # 如果要帶着cookie 可以傳入cookie,也可以放在報文頭當中
12     #'Cookie':'這里放入cookie'
13 } 14 
15 # 1 要爬取的頁面地址
16 url = 'http://www.baidu.com'
17 # 2 發送get請求 拿到響應
18 response = requests.get(url=url,cookies=cookies) 19 #response = requests.get(url=url,headers=headers)
20 # 3 獲取響應內容文本 兩種方法
21 html = response.content.decode() #response.content為bytes類型,decode() 將它轉換為utf8
22 print(html)

 

7 保持session 幫我們保存response中的session

 1 import requests  2 # 獲取一個session對象為我們發送請求 用法與requests對象相同
 3 session = requests.session()  4 
 5 url = 'http://www.baidu.com'
 6 #保持session發送請求
 7 response = session.get(url=url)  8 # 獲取頁面
 9 html = response.content.decode() 10 print(html) 11 #查看session
12 print(response.cookies)

 

8 設置連接超時時間

 1 import requests  2 # 獲取一個session對象為我們發送請求 用法與requests對象相同
 3 session = requests.session()  4 
 5 url = 'http://www.baidu.com'
 6 #保持session發送請求
 7 response = session.get(url=url,timeout = 3) # 3秒時間為超時時間
 8 # 獲取頁面
 9 html = response.content.decode() 10 print(html) 11 #查看session
12 print(response.cookies)

 

9 設置ssl校驗 對方https協議合法性是否忽略

 1 import requests  2 # 獲取一個session對象為我們發送請求 用法與requests對象相同
 3 session = requests.session()  4 
 5 url = 'http://www.baidu.com'
 6 #保持session發送請求
 7 response = session.get(url=url,verify=False) # 不校驗ssl 如果對方https協議不合法,我們忽略 繼續請求
 8 # 獲取頁面
 9 html = response.content.decode() 10 print(html) 11 #查看session
12 print(response.cookies)

 

10 重新連接次數

 1 import requests  2 from retrying import retry  3 
 4 
 5 @retry(stop_max_attempt_number=3) # 設置超時重新連接 次數3
 6 def get( url ):  7     response = requests.get(url=url,timeout=3)  8     return response.content.decode()  9 
10 url = 'http://www.baidu.com'
11 html = get(url) 12 print(html)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM