網(wǎng)站首頁 編程語言 正文
前言
記得我剛學(xué)python-requests庫的時候總會有點暈,于是我做了以下關(guān)于requests庫的知識點整理,方便初學(xué)者可以更好的了解requests庫。如果有補充或錯誤,或者不懂的地方,可以評論區(qū)留言。
1、Requests介紹
Requests是Python一個很實用的HTTP客戶端,完全滿足如今網(wǎng)絡(luò)爬蟲的需求
urllib庫和requests庫功能類似,但requests庫功能更多更實用
2、requests庫的安裝
pip命令安裝(方法一)
- windows操作系統(tǒng):pip install requests
- Mac操作系統(tǒng):pip3 install requests
- Linux操作系統(tǒng):sodo pip install requests
源碼安裝(方法二)
- 下載 requests源碼 http://mirrors.aliyun.com/pypi/simple/ requests/
- 下載文件到本地之后,解壓到Python安裝目錄,之后打開解壓文
- 運行命令行輸入python setup.py install 即可安裝
測試
- import requests
- 如果沒提示錯誤,那說明已經(jīng)安裝成功了!
3、requests庫常用的方法
序號 |
方法 |
描述 |
1 |
requests.request(url) |
構(gòu)造一個請求,支持以下各種方法 |
2 |
requests.get() |
發(fā)送一個Get請求 |
3 |
requests.post() |
發(fā)送一個Post請求 |
4 |
requests.head() |
獲取HTML的頭部信息 |
5 |
requests.put() |
發(fā)送Put請求 |
6 |
requests.patch() |
提交局部修改的請求 |
7 |
requests.delete() |
提交刪除請求 |
最常用的方法為get()和post()分別用于發(fā)送Get請求和Post請求
4、response對象的常用屬性
序號 |
屬性或方法 |
描述 |
1 |
response.status_code |
響應(yīng)狀態(tài)碼 |
2 |
response.content |
把response對象轉(zhuǎn)換為二進制數(shù)據(jù) |
3 |
response.text |
把response對象轉(zhuǎn)換為字符串數(shù)據(jù) |
4 |
response.encoding |
定義response對象的編碼 |
5 |
response.cookie |
獲取請求后的cookie |
6 |
response.url |
獲取請求網(wǎng)址 |
7 |
response.json() |
內(nèi)置的JSON解碼器 |
8 |
Response.headers |
以字典對象存儲服務(wù)器響應(yīng)頭,字典鍵不區(qū)分大小寫 |
5、使用requests發(fā)送get請求
- 不帶參數(shù)的get請求
- 案例:爬取百度主頁
- 帶參數(shù)的get請求
- 案例:貼吧
- 獲取JSON數(shù)據(jù)
- 案例:百度美女圖片
- 獲取二進制數(shù)據(jù)
- 案例:下載百度logo
5.1 ?不帶參數(shù)的get請求
# 不帶參數(shù)的get請求
import requests
url='http://www.baidu.com'
resp = requests.get(url)
# 設(shè)置響應(yīng)的經(jīng)編碼格式
resp.encoding='utf-8'
cookie=resp.cookies # 獲取請求后的cookie信息
headers=resp.headers
print('響應(yīng)狀態(tài)碼:', resp.status_code)
print('請求后的cookie:', cookie)
print('獲取請求的網(wǎng)址:', resp.url)
print('響應(yīng)頭:', headers)
print('響應(yīng)內(nèi)容', resp.text)
----------------------------------以下為輸出結(jié)果----------------------------------
'''
響應(yīng)狀態(tài)碼: 200
請求后的cookie: <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
獲取請求的網(wǎng)址: http://www.baidu.com/
響應(yīng)頭: {'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'}
響應(yīng)內(nèi)容 <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type.........
'''
5.2 帶參數(shù)的get請求
5.2.1 查詢參數(shù)params
- params,數(shù)據(jù)類型為字典
- 作用:對URL地址中的查詢參數(shù)自動進行編碼拼接
- 使用示例:resp = requests.get(url=baseurl, params=params, headers=headers)
# 帶參數(shù)的get請求
import requests
url = 'https://tieba.baidu.com/f?'
params = {'kw':'大學(xué)吧', '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證書認證參數(shù) verify
- 參數(shù)值:True(默認)| False
- 適用網(wǎng)站:https類型網(wǎng)站但是沒有經(jīng)過 證書認證機構(gòu) 認證的網(wǎng)站
- 適用場景:當程序中拋出SSLError異常則考慮使用此參數(shù)
- 使用示例:requests.get(url=url,headers=headers,verify=False)
- 當verify參數(shù)設(shè)置為False時,則不會再對網(wǎng)站進行SSL證書認證
5.2.3 設(shè)置超時時間 timeout
我們可以通過timeout屬性設(shè)置超時時間,一旦超過這個時間還沒獲得響應(yīng)內(nèi)容,就會提示錯誤。
import requests
requests.get('http://github.com', timeout=0.001)
---------------------以下為輸出結(jié)果(報錯)---------------------
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參數(shù) proxies
5.2.4.1 免費代理IP
- 語法格式:proxies = { '協(xié)議':'協(xié)議://IP:端口號'}
- 示例:
- 當我們抓取的地址為http時,則會選擇proxies中http的代理,反之為https
import requests
url = 'http://httpbin.org/get'
headers = {'User-Agent':'Mozilla/5.0'}
# 定義代理,再代理IP網(wǎng)站中查找免費代理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 = { '協(xié)議':'協(xié)議://用戶名:密碼@IP:端口號'}
示例:
5.3 獲取JSON數(shù)據(jù)
# 獲取json數(shù)據(jù)
# 案例:百度獲取宮崎駿動漫圖片
# 滑動頁面,URL沒變化,F(xiàn)12中的文件越來越多,說明這是動態(tài)網(wǎng)頁
# 選擇XHR中的一個,復(fù)制其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 獲取二進制數(shù)據(jù)
一般來說,對于非文本請求,可以以字節(jié)形式訪問響應(yīng)正文。
# 獲取二進制數(shù)據(jù)
# 案例:保存百度圖片
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對象轉(zhuǎn)換為二進制數(shù)據(jù)
file.write(resp.content)
6、使用requests發(fā)送post請求
-
語法結(jié)構(gòu)
- requests.post(url, data=None, json=None)
-
參數(shù)說明
- url:需要爬取的網(wǎng)站的網(wǎng)址
- data:請求數(shù)據(jù)
- json:json格式的數(shù)據(jù)
- 案例:登錄小說樓
- 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('響應(yīng)狀態(tài)碼:', resp.status_code) # 200
print('響應(yīng)內(nèi)容', resp.text) # <html>......</html>
7、使用requests的session發(fā)送請求
import requests
url='https://www.xslou.com/login.php'
data={'username':'18600605736', 'password':'57365736', 'action':'login'}
# 使用session發(fā)送請求
session = requests.session()
resp=session.post(url,data=data) # 使用session發(fā)送post請求
resp.encoding='gb2312'
# print( resp.text) # <html>..<title>登錄成功</title>....</html>
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_45932368/article/details/121508547
相關(guān)推薦
- 2022-12-25 使用Python可設(shè)置抽獎?wù)邫?quán)重的抽獎腳本代碼_python
- 2022-06-02 Android超詳細講解組件LinearLayout的使用_Android
- 2022-12-23 Android開發(fā)之線程通信詳解_Android
- 2022-03-24 聊一聊redis奇葩數(shù)據(jù)類型與集群知識_Redis
- 2022-07-12 k8s conntrack 表項超時導(dǎo)致tcp長連接中斷
- 2022-07-22 idea 編譯項目后target包沒有resources文件
- 2022-06-29 C#集合之自定義集合類_C#教程
- 2022-10-01 Android11及以上文件讀寫權(quán)限申請詳細介紹_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支