一、安裝
1.pip安裝
windows系統下只需要在命令列輸入命令 pip install requests 即可安裝
在 linux 系統下,只需要輸入命令 sudo pip install requests ,即可安裝。
2.安裝包安裝
下載地址:https://github.com/requests/requests
運行命令列并輸入:python setup.py install
3.pychrom安裝
File->Settings->Project:python->Project Interpreter->Install ->在搜索框搜索requests->
Install Package
Specify version 指定版本
Options 指定安裝路徑
4.專案匯入:import requests
二、requests模塊的使用方法
1.常見的請求方式
(1).GET: 請求指定的頁面資訊,并回傳物體主體。
(2).HEAD: 只請求頁面的首部。
(3).POST: 請求服務器接受所指定的檔案作為對所標識的URI的新的從屬物體。
(4).PUT: 從客戶端向服務器傳送的資料取代指定的檔案的內容。
(5).DELETE: 請求服務器洗掉指定的頁面。
注:get 和 post比較常見 GET請求將提交的資料放置在HTTP請求協議頭中,POST提交的資料則放在物體資料中
2.requests庫的七個主要方法
方法 解釋
requests.request() 構造一個請求,支持以下各種方法
requests.get() 獲取html的主要方法
requests.head() 獲取html頭部資訊的主要方法
requests.post() 向html網頁提交post請求的方法
requests.put() 向html網頁提交put請求的方法
requests.patch() 向html提交區域修改的請求
requests.delete() 向html提交洗掉請求
3.requests.get()的決議
r=requests.get(url,params,**kwargs)
url: 需要爬取的網站地址
例:
r = requests.get('http://httpbin.org/get')結果:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5ed75df5-526882d5f959312833b6b506"
},
"origin": "125.121.233.25",
"url": "http://httpbin.org/get"
}
params: 翻譯過來就是引數, url中的額外引數,字典或者位元組流格式,可選
例:
params = {'name': 'Zy'}
r = requests.get('http://httpbin.org/get', params=params)
print(r.text)結果:
{
"args": {
"name": "Zy"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5ed75d3e-84578248cd064904f5c1501c"
},
"origin": "125.121.233.25",
"url": "http://httpbin.org/get?name=Zy"
}
**kwargs : 12個控制訪問的引數
**kwargs的引數詳情
(1). params:字典或位元組序列, 作為引數增加到url中,使用這個引數可以把一些 鍵值對以?key1=value1&key2=value2的模式增加到url中
(2). data:字典,位元組序或檔案物件,重點作為向服務器提供或提交資源是提交,,作為request的內容,與params不同的是,data提交的資料并不放在url鏈接里, 而是放在url鏈接對應位置的地方作為資料來存盤。,它也可以接受一個字串物件。
例:post請求中,一般資料都會在請求報文中,不會在URL中直接展示,就以post請求演示
data = 'Hello World'
r = requests.post('http://httpbin.org/post', data=https://bbs.csdn.net/topics/data)
print(r.text)
結果:
{
"args": {},
"data": "Hello World",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "11",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5ed760ca-9f048ddcee9f29143586b9d0"
},
"json": null,
"origin": "125.121.233.25",
"url": "http://httpbin.org/post"
}
(3). json:json格式的資料, json合適在相關的html,http相關的web開發中非常常見, 也是http最經常使用的資料格式, 他是作為內容部分可以向服務器提交。
例:
josn = {
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
}
}
r = requests.post('http://httpbin.org/post', json=josn)
print(r.text)結果:
{
"args": {},
"data": "{\"pages\": [\"pages/index/index\", \"pages/logs/logs\"], \"window\": {\"backgroundTextStyle\": \"light\", \"navigationBarBackgroundColor\": \"#fff\", \"navigationBarTitleText\": \"WeChat\", \"navigationBarTextStyle\": \"black\"}}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "206",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5ed774f6-592ec426b9aea6b0c689ca7e"
},
"json": {
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "WeChat"
}
},
"origin": "125.121.233.25",
"url": "http://httpbin.org/post"
}
(4). headers:字典是http的相關語,對應了向某個url訪問時所發起的http的頭欄位, 可以用這個欄位來定義http的訪問的http頭,可以用來模擬任何我們想模擬的瀏覽器來對url發起訪問。
注:一般是用來避免網站反爬蟲,headers包含一些瀏覽器資訊,可以通過添加headers來簡單的繞過
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}
r = requests.post('http://httpbin.org/post', headers=headers)
print(r.text)結果:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5ed7781e-9476ac9ed0bdd93e681c77ca"
},
"json": null,
"origin": "125.121.233.25",
"url": "http://httpbin.org/post"
}
(5). cookies:字典或CookieJar,指的是從http中決議cookie
注:Cookie在客戶端與服務器端進行互動,最終實作對用戶身份的認證,Cookie成了用戶整個身份的代替
cookie = {'set-cookie': 'ID=cookie; Path=/'}
r = requests.get('http://httpbin.org/cookies', cookies=cookie)
print(r.text)結果:
{
"cookies": {
"set-cookie": "ID=cookie"
}
}
(6). auth:元組,用來支持http認證功能
(7). files:字典, 是用來向服務器傳輸檔案時使用的欄位
(8). timeout: 用于設定超時時間, 單位為秒,當發起一個get請求時可以設定一個timeout時間, 如果在timeout時間內請求內容沒有回傳, 將產生一個timeout的例外。
(9). proxies:字典, 用來設定訪問代理服務器
(10). allow_redirects: 開關, 表示是否允許對url進行重定向, 默認為True
(11). stream: 開關, 指是否對獲取內容進行立即下載, 默認為True
(12). verify:開關, 用于認證SSL證書, 默認為True
(13). cert: 用于設定保存本地SSL證書路徑
4.response物件的屬性
屬性 說明
r.status_code http請求的回傳狀態,若為200則表示請求成功。
r.text http回應內容的字串形式,即回傳的頁面內容
r.encoding 從http header 中猜測的相應內容編碼方式
r.apparent_encoding 從內容中分析出的回應內容編碼方式(備選編碼方式)
r.content http回應內容的二進制形式
演示:
cookie = {'set-cookie': 'ID=cookie; Path=/'}
r = requests.get('http://httpbin.org/cookies', cookies=cookie)
print(r.status_code)
print(r.text)
print(r.encoding)
print(r.apparent_encoding)
print(r.content)結果:
200
{
"cookies": {
"set-cookie": "ID=cookie"
}
}
None
ascii
b'{\n "cookies": {\n "set-cookie": "ID=cookie"\n }\n}\n'
第一次發帖,小白,什么都不懂,有錯誤請指出,謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/38559.html
