目錄
requests常用函數屬性
requests.get # 模擬發送get請求
requests.post # 模擬發送post請求
requests.session # 相當於自動幫我們提交cookie的requests
# eg:
url = "https://www.baidu.com"
response = requests.get(url=url)
requests常用函數的常用參數
get請求函數的常用參數 params參數 headers參數
# params參數
response = requests.get(
url="https://www.baidu.com",
headers={"User-Agent":User_Agent}, # 請求頭攜帶的數據
params={"key":'value'}, # get請求攜帶數據
)
post請求函數的常用參數 data,json參數
# data,json參數
response = requests.post(
url=url,
data={"name":"lxx","pwd":"123"}, # 相當於請求體攜帶數據
json={"name":"jerry","pwd":"123"}, # 直接序列化成字符串
)
代理池 proxies參數
# 代理池
ps = ["121.228.240.101:9999","121.228.240.101:9999","121.228.240.101:9999","121.228.240.101:9999"] # 裏面一堆代理服務器
response = requests.post(
url="http://news.baidu.com/?tn=news",
proxies={"HTTP":random.choice(ps)}, # 服務器,從代理池裏隨機選一個
)
with open("ttt.html","wb") as f:
f.write(response.content)
print(resp.text)
超時時間 timeout參數
response = requests.post(
"https://www.baidu.com",
timeout=(10,10)
)
# timeout=(10,10),第一個參數表示請求連接超時時間,第二個參數表示響應超時時間
# timeout=10 代表超時時間
是否允許重定向 allow_redirects參數
response = requests.post(
url=url,
allow_redirects = True, # 是否允許重定向,默認爲True
)
上傳文件 files參數
f = open(r"D:\jerry\spiderDay2\ttt.html","rb")
# 接收一個字典 key是服務器用於提取文件的字段名 f時要上傳的文件對象
response = requests.post(
url="http://httpbin.org/post",
files={"img":f} # key只能爲img
)
print(response.status_code)
verify 證書驗證 目前已經基本不用了 都換成了HTTPS 請求
import requests
response=requests.get(
url='https://www.12306.cn',
cert=('/path/server.crt','/path/key'), # 表示使用哪些證書驗證,作爲服務端驗證
# cert=('/path/client.crt','/path/key'), # 表示使用哪些證書驗證,作爲客戶端驗證
verify=True # True 表示使用證書驗證
)
print(response.status_code)
#改進1:去掉報錯,但是會報警告
import requests
response=requests.get(
'https://www.12306.cn',
verify=False # 不驗證證書,報警告,返回200
)
print(respone.status_code)
#改進2:去掉報錯,並且去掉警報信息
import requests
from requests.packages import urllib3
urllib3.disable_warnings() # 關閉警告
response=requests.get(
'https://www.12306.cn',verify=False ) # verify=False,不驗證證書,報警告,返回200
print(response.status_code)
異常處理
import requests
from requests.exceptions import * #可以查看requests.exceptions獲取異常類型
try:
r=requests.get('http://www.baidu.com',timeout=0.00001)
except ReadTimeout:
print('===:')
except ConnectionError: # 鏈接失敗
print('-----')
except Timeout: # 超時
print('aaaaa')
except RequestException: # 其他異常
print('Error')
關閉:上下文管理,對於不支持使用"with"語句的類似文件的對象,使用 contextlib.closing():
from contextlib import closing
with closing(requests.get('http:xxxxxxxxx.com', stream=True)) as r:
for line in response.iter_content():
print(line)
response的常用屬性
url = "https://www.baidu.com/s"
response = requests.get(
url=url, # 請求的路由
params={"wd":"egon"}, # get攜帶數據
headers={"user-agent": user_agent} # 請求頭攜帶數據
)
# 響應的常用屬性
response.text # 響應回去的文本(字符串)
response.content # 響應回去的內容(二進制),一般用來爬取視頻
response.status_code # 響應的狀態碼
response.url # 獲取請求連接地址
response.cookies # 獲取返回的cookies信息
response.cookies.get_dict() # 獲取返回的cookies信息
response.request # 獲取請求方式
# 放回結果爲json數據的
response.json() # 將結果進行反序列化
# 爬取文檔亂碼問題
response.apparent_encoding # 文檔的編碼的方式(從HTML文檔找)
response.encoding # 響應體編碼方式
eg: response.encoding = response.apparent_encoding # 文檔的聲明方式
response.headers # 查看響應頭
response.history # 重定向歷史 即前一次請求的地址
# 流的原始數據(報頭的方式)
response = requests.get(
url=url,
params={"wd":"egon"},
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3538.77 Safari/537.36"},
stream=True # 流的原始數據要設置爲 True 才能讀取
)
# 以流的方式讀取原始數據 沒有經過HTTP協議解析的數據 一般不用
print(resp.raw.read(100))