request基本使用及各種請求方式參數(shù)的示例
各種請求方式
pip3 install requests >>> import requests >>> r = requests.get('https://www.cnblogs.com') >>> r = requests.post('https://www.cnblogs.com', data = {'key':'value'}) >>> r = requests.put('https://www.cnblogs.com', data = {'key':'value'}) >>> r = requests.delete('https://www.cnblogs.com') >>> r = requests.head('https://www.cnblogs.com') >>> r = requests.options('https://www.cnblogs.com')
request各種參數(shù)
參數(shù) | 說明 |
---|---|
params | 字典或字節(jié)序列,作為參數(shù)增加到url中 |
data | 字典,字節(jié)序列或文件對象,作為request的內(nèi)容 |
json | JSON格式的數(shù)據(jù),作為request的內(nèi)容 |
headers | 字典,HTTP定制頭 |
cookies | 字典或CookieJar, request中的cookie |
auth | 元組,支持HTTp認(rèn)證功能 |
files | 字典類型,傳輸文件 |
timeout | 設(shè)定超時時間,秒為單位 |
proxies | 字典類型,設(shè)定訪問代理服務(wù)器,可以增加登錄認(rèn)證 |
allow_redirects | 重定向開關(guān),默認(rèn)為True |
stream | 獲取內(nèi)容立即下載開關(guān),默認(rèn)為True |
verify | 認(rèn)證SSL證書開關(guān),默認(rèn)為True |
cert | 本地SSL證書路徑 |
request基本使用
import requests response = requests.get('https://www.cnblogs.com/kermitjam/') print(response.text) print(respone.text) # 響應(yīng)體轉(zhuǎn)成str print(respone.content) # 響應(yīng)體二進(jìn)制(圖片,視頻) print(respone.status_code) # 響應(yīng)狀態(tài)碼 print(respone.headers) # 響應(yīng)頭 print(respone.cookies) # 服務(wù)端返回的cookie print(respone.cookies.get_dict()) # 轉(zhuǎn)成字典 print(respone.cookies.items()) print(respone.url) # 當(dāng)次請求的地址 print(respone.history) # 如果有重定向,放到一個列表中.查看上一次請求從哪里來。(圖片防盜鏈) print(respone.encoding) # 編碼方式 print(respone.iter_content()) # 視頻,圖片迭代取值 content-length # 查看下載前文件大小 response.iter_content() # 視頻,圖片迭代取值 with open("a.mp4",'wb') as f: for line in response.iter_content(): f.write(line)
get請求攜帶參數(shù)
# 方式一:會轉(zhuǎn)碼,不推薦 response = requests.get('https://www.baidu.com/s?wd=%E7%BE%8E%E5%A5%B3') # 方式二:推薦 response = requests.get('https://www.baidu.com/',params={'name':'美女','age':18})
get請求攜帶headers頭
respone = requests.get('http://127.0.0.1:8000/',params={'name':'美女','age':18}, headers={ # 標(biāo)志,什么東西發(fā)出的請求,瀏覽器信息,django框架,從哪取?(meta) 'User-Agent': 'request', # 上一個頁面的地址,圖片防盜鏈 'Referer': 'xxx' }) print(respone.status_code)
get請求攜帶cookie
帶cookie,隨機(jī)字符串(用戶信息:也代表session),不管后臺用的token認(rèn)證,還是session認(rèn)證
一旦登陸了,帶著cookie發(fā)送請求,表示登陸了(下單,12306買票,評論)
# 第一種方式 ret = requests.get('http://127.0.0.1:8000/',params={'name':'美女','age':18}, headers={ 'cookie': 'key3=value;key2=value', }) # 第二種方式 ret = requests.get('http://127.0.0.1:8000/',params={'name':'美女','age':18}, cookies={"islogin":"xxx"}) print(ret.status_code)
post請求攜帶數(shù)據(jù)(注冊、登錄)
# data:urlencoded編碼 ret = requests.post('http://127.0.0.1:8000/', data={'name': "jeff", 'age': 18}) # data:json編碼 import json data = json.dumps({'name': "jeff", 'age': 18}) ret = requests.post('http://127.0.0.1:8000/', json=data) print(ret) # 注意:編碼格式是請求頭中帶的,所以可以手動修改,在headers中改
session對象
# session對象 session=requests.session() # 跟requests.get/post用起來完全一樣,但是它處理了cookie # 假設(shè)是一個登陸,并且成功 res = session.post('http://127.0.0.1:8000/') # # 再向該網(wǎng)站發(fā)請求,就是登陸狀態(tài),不需要手動攜帶cookie res = session.get("http://127.0.0.1:8000/") print(res)
亂碼問題
# 加載回來的頁面,打印出來,亂碼(我們用的是utf8編碼),如果網(wǎng)站用gbk, ret = requests.get('http://127.0.0.1:8000/') ret.encoding='gbk' # 修改編碼 print(ret.apparent_encoding) # 當(dāng)前頁面的編碼 # ret.encoding = ret.apparent_encoding print(ret.encoding) print(ret.apparent_encoding)
解析json數(shù)據(jù)
# 返回數(shù)據(jù),有可能是json格式,有可能是html格式 ret=requests.get('http://127.0.0.1:8000/') # print(type(ret.text)) # print(ret.text) a=ret.json() print(a['name']) print(type(a))
使用代理
代理簡單解釋:
代理其實就是一個中介,A和B本來可以直連,中間插入一個C,C就是中介。
#高匿:服務(wù)端,根本不知道我是誰
#普通:服務(wù)端是能夠知道我的ip的
#http請求頭中:X-Forwarded-For:代理的過程
爬蟲使用正向代理好處:
1.突破頻率限制。做個代理池,每次請求都是不同的Ip,服務(wù)器認(rèn)為是不同的用戶
2.不會封自己的ip。
正向代理即是客戶端代理, 代理客戶端, 服務(wù)端不知道實際發(fā)起請求的客戶端.
反向代理即是服務(wù)端代理, 代理服務(wù)端, 客戶端不知道實際提供服務(wù)的服務(wù)端
ret = requests.get('https://www.pearvideo.com/', proxies={'http': '47.115.54.89'}) print(type(ret.text)) print(ret.text)
上傳文件
# 爬蟲用的比較少,后臺寫服務(wù) file = {'myfile': open("1.txt", 'rb')} ret = requests.post('http://127.0.0.1:8000/', files=file) print(ret.content)
以上就是request基本使用及各種請求方式參數(shù)的示例的詳細(xì)內(nèi)容,更多關(guān)于request使用及請求方式參數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中使用logging和traceback模塊記錄日志和跟蹤異常
今天小編就為大家分享一篇關(guān)于Python中使用logging和traceback模塊記錄日志和跟蹤異常,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04django admin后臺添加導(dǎo)出excel功能示例代碼
這篇文章主要介紹了django admin 后臺添加導(dǎo)出excel功能示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的方法實例
這篇文章主要給大家介紹了關(guān)于Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的相關(guān)資料,好的圖表能幫助我們深化數(shù)據(jù)的記憶點,文中通過圖文以及代碼示例將實現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12pandas按行按列遍歷Dataframe的三種方式小結(jié)
本文主要介紹了pandas按行按列遍歷Dataframe,主要介紹了三種方法,具有一定的參考價值,感興趣的可以了解一下2023-11-11