網站首頁 編程語言 正文
前言
記得我剛學python-requests庫的時候總會有點暈,于是我做了以下關于requests庫的知識點整理,方便初學者可以更好的了解requests庫。如果有補充或錯誤,或者不懂的地方,可以評論區留言。
1、Requests介紹
Requests是Python一個很實用的HTTP客戶端,完全滿足如今網絡爬蟲的需求
urllib庫和requests庫功能類似,但requests庫功能更多更實用
2、requests庫的安裝
pip命令安裝(方法一)
- windows操作系統:pip install requests
- Mac操作系統:pip3 install requests
- Linux操作系統:sodo pip install requests
源碼安裝(方法二)
- 下載 requests源碼 http://mirrors.aliyun.com/pypi/simple/ requests/
- 下載文件到本地之后,解壓到Python安裝目錄,之后打開解壓文
- 運行命令行輸入python setup.py install 即可安裝
測試
- import requests
- 如果沒提示錯誤,那說明已經安裝成功了!
3、requests庫常用的方法
序號 |
方法 |
描述 |
1 |
requests.request(url) |
構造一個請求,支持以下各種方法 |
2 |
requests.get() |
發送一個Get請求 |
3 |
requests.post() |
發送一個Post請求 |
4 |
requests.head() |
獲取HTML的頭部信息 |
5 |
requests.put() |
發送Put請求 |
6 |
requests.patch() |
提交局部修改的請求 |
7 |
requests.delete() |
提交刪除請求 |
最常用的方法為get()和post()分別用于發送Get請求和Post請求
4、response對象的常用屬性
序號 |
屬性或方法 |
描述 |
1 |
response.status_code |
響應狀態碼 |
2 |
response.content |
把response對象轉換為二進制數據 |
3 |
response.text |
把response對象轉換為字符串數據 |
4 |
response.encoding |
定義response對象的編碼 |
5 |
response.cookie |
獲取請求后的cookie |
6 |
response.url |
獲取請求網址 |
7 |
response.json() |
內置的JSON解碼器 |
8 |
Response.headers |
以字典對象存儲服務器響應頭,字典鍵不區分大小寫 |
5、使用requests發送get請求
- 不帶參數的get請求
- 案例:爬取百度主頁
- 帶參數的get請求
- 案例:貼吧
- 獲取JSON數據
- 案例:百度美女圖片
- 獲取二進制數據
- 案例:下載百度logo
5.1 ?不帶參數的get請求
# 不帶參數的get請求
import requests
url='http://www.baidu.com'
resp = requests.get(url)
# 設置響應的經編碼格式
resp.encoding='utf-8'
cookie=resp.cookies # 獲取請求后的cookie信息
headers=resp.headers
print('響應狀態碼:', resp.status_code)
print('請求后的cookie:', cookie)
print('獲取請求的網址:', resp.url)
print('響應頭:', headers)
print('響應內容', resp.text)
----------------------------------以下為輸出結果----------------------------------
'''
響應狀態碼: 200
請求后的cookie: <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
獲取請求的網址: http://www.baidu.com/
響應頭: {'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 23 Apr 2021 00:10:35 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:16 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}
響應內容 <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type.........
'''
5.2 帶參數的get請求
5.2.1 查詢參數params
- params,數據類型為字典
- 作用:對URL地址中的查詢參數自動進行編碼拼接
- 使用示例:resp = requests.get(url=baseurl, params=params, headers=headers)
# 帶參數的get請求
import requests
url = 'https://tieba.baidu.com/f?'
params = {'kw':'大學吧', 'pn':'3'}
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64)'}
# 開始請求
html = requests.get(url=url, params=params, headers=headers).text
print(html)
5.2.2 SSL證書認證參數 verify
- 參數值:True(默認)| False
- 適用網站:https類型網站但是沒有經過 證書認證機構 認證的網站
- 適用場景:當程序中拋出SSLError異常則考慮使用此參數
- 使用示例:requests.get(url=url,headers=headers,verify=False)
- 當verify參數設置為False時,則不會再對網站進行SSL證書認證
5.2.3 設置超時時間 timeout
我們可以通過timeout屬性設置超時時間,一旦超過這個時間還沒獲得響應內容,就會提示錯誤。
import requests
requests.get('http://github.com', timeout=0.001)
---------------------以下為輸出結果(報錯)---------------------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
5.2.4 代理IP參數 proxies
5.2.4.1 免費代理IP
- 語法格式:proxies = { '協議':'協議://IP:端口號'}
- 示例:
- 當我們抓取的地址為http時,則會選擇proxies中http的代理,反之為https
import requests
url = 'http://httpbin.org/get'
headers = {'User-Agent':'Mozilla/5.0'}
# 定義代理,再代理IP網站中查找免費代理IP
proxies = {
'http':'http://112.85.164.220:9999',
'https':'https://112.85.164.220:9999'
}
html = requests.get(url=url,proxies=proxies,headers=headers,timeout=5).text
print(html)
5.2.4.1 私密代理和獨享代理
語法格式:proxies = { '協議':'協議://用戶名:密碼@IP:端口號'}
示例:
5.3 獲取JSON數據
# 獲取json數據
# 案例:百度獲取宮崎駿動漫圖片
# 滑動頁面,URL沒變化,F12中的文件越來越多,說明這是動態網頁
# 選擇XHR中的一個,復制其Request URL,粘貼給url
import requests
url='https://image.baidu.com/search/acjson?tn=resultjson_com&logid=10167214135414424439&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%AE%AB%E5%B4%8E%E9%AA%8F%E5%8A%A8%E6%BC%AB%E5%9B%BE%E7%89%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&word=%E5%AE%AB%E5%B4%8E%E9%AA%8F%E5%8A%A8%E6%BC%AB%E5%9B%BE%E7%89%87&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1619134335166='
resp=requests.get(url)
json_data=resp.json()
print(json_data)
5.4 獲取二進制數據
一般來說,對于非文本請求,可以以字節形式訪問響應正文。
# 獲取二進制數據
# 案例:保存百度圖片
import requests
url='https://www.baidu.com/img/bd_logo1.png'
resp=requests.get(url)
# 存儲
with open('logo.png','wb') as file:
# resp.content:把response對象轉換為二進制數據
file.write(resp.content)
6、使用requests發送post請求
-
語法結構
- requests.post(url, data=None, json=None)
-
參數說明
- url:需要爬取的網站的網址
- data:請求數據
- json:json格式的數據
- 案例:登錄小說樓
- https://www.xslou.com/login.php
import requests
url='https://www.xslou.com/login.php'
data={'username':'18600605736', 'password':'57365736', 'action':'login'}
resp = requests.post(url,data)
resp.encoding='gb2312'
print('響應狀態碼:', resp.status_code) # 200
print('響應內容', resp.text) # <html>......</html>
7、使用requests的session發送請求
import requests
url='https://www.xslou.com/login.php'
data={'username':'18600605736', 'password':'57365736', 'action':'login'}
# 使用session發送請求
session = requests.session()
resp=session.post(url,data=data) # 使用session發送post請求
resp.encoding='gb2312'
# print( resp.text) # <html>..<title>登錄成功</title>....</html>
總結
原文鏈接:https://blog.csdn.net/weixin_45932368/article/details/121508547
相關推薦
- 2023-01-26 C#實現Word轉換TXT的方法詳解_C#教程
- 2022-06-22 android使用intent傳遞參數實現乘法計算_Android
- 2022-06-01 C#的通用DbHelper類(支持數據連接池)示例詳解_C#教程
- 2022-04-21 詳解Golang?Map中的key為什么是無序的_Golang
- 2023-07-16 spring boot 實現token攔截
- 2022-06-16 Python中弱引用的神奇用法與原理詳解_python
- 2022-12-26 層次分析法在matlab上的實現方式_python
- 2022-12-12 React?高階組件與Render?Props優缺點詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支