5分鐘入門一個(gè)python庫(kù)——requests

image

Hello, 大家好,今天小 Geek 給大家介紹的是一個(gè)第三方庫(kù) <requests>,什么來(lái)歷呢?我們先來(lái)看看它有哪些tag:

全球下載量超3億次 python必備庫(kù)

爬蟲利器 接口測(cè)試必會(huì) For Human Beings

而它的作者就是下面這位顏值擔(dān)當(dāng)了— Kenneth Reitz

image

最基礎(chǔ)的用法(DOC)

不管怎么樣,我們先來(lái)安裝它,畢竟它還不是 python 標(biāo)準(zhǔn)庫(kù)的一份子呢

pip install requests

使用 Requests 發(fā)送網(wǎng)絡(luò)請(qǐng)求

import requests
response = requests.get('http://httpbin.org/get')

通過(guò) requests.get 方法,入?yún)魅胍粋€(gè) URL,就可以獲取一個(gè) response 對(duì)象,如果你想看看網(wǎng)絡(luò)請(qǐng)求返回的 response 對(duì)象是什么內(nèi)容的話,可以使用 response.content 或 response.text 屬性查看

print(response.text)
{
    "args": {},
    "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
        "Host": "httpbin.org",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
    },
    "origin": "219.133.101.172, 219.133.101.172",
    "url": "https://httpbin.org/get"
}

好了,是時(shí)候正式上路了

接下來(lái)給大家介紹下如何應(yīng)對(duì) HTTP 請(qǐng)求的各種方法,請(qǐng)系好安全帶??

GET 請(qǐng)求

其實(shí)在上面的基礎(chǔ)用法中已經(jīng)展示了 GET 請(qǐng)求怎么寫,只是沒(méi)有帶參數(shù),那么我們?cè)賮?lái)看看帶參數(shù)的 GET 請(qǐng)求怎么玩

GET 示例:http://jsonplaceholder.typicode.com/posts?userId=1&id=2
params = {
   "userId": 1,
   "id": 2
 }
 url = "http://jsonplaceholder.typicode.com/posts"
 response = requests.get(url, params=params)

GET 請(qǐng)求的參數(shù)就是 ? 后面以 & 連接的鍵值對(duì),你只需要把一個(gè)字典對(duì)象傳給 requests.get 方法中的 params 參數(shù)即可, requests 庫(kù)會(huì)默默幫你將字典對(duì)象進(jìn)行 URL encode ,然后拼接成完整的 URL

POST 請(qǐng)求

POST 請(qǐng)求也是 HTTP 中最常用的請(qǐng)求方式之一,這里將介紹兩種類型的 POST Body 體,分別是:application/x-www-form-urlencodedapplication/json(繼續(xù)往下看哦,開(kāi)始畫重點(diǎn))

POST 示例:http://httpbin.org/post
# Content-Type: application/x-www-form-urlencoded
data = {
   "userId": 1,
   "id": 2
 }
 url = "http://httpbin.org/post"
 response = requests.post(url, data=data)

注意,這里跟 GET 請(qǐng)求參數(shù)名是不一樣的,在 POST 請(qǐng)求中傳參給data 參數(shù),requests 默認(rèn)會(huì)把字典對(duì)象參數(shù)序列化為字符串參數(shù),如上面的 data 參數(shù)會(huì)轉(zhuǎn)換為 userId=1&id=2,聰明的同學(xué)可能發(fā)現(xiàn)了,這不是跟 GET 請(qǐng)求一樣?

還是不一樣的,俗話說(shuō)得好,看事不要光看外表對(duì)吧。POST 參數(shù)轉(zhuǎn)換后會(huì)放在 Body 體,且在 request headers 中要求注明 Content-Type,當(dāng)然這個(gè)步驟 requests 庫(kù)也會(huì)默默幫你做的,否則都對(duì)不起 For Human Beings

POST 第二種 Body 體類型 application/json, 這也是目前 HTTP 接口最流行的一種 Body 數(shù)據(jù)類型

# Content-Type: application/json
data = {
   "userId": 1,
   "id": 2
 }
 url = "http://httpbin.org/post"
 # 注意這里參數(shù)名不是 data 了 ,對(duì),json, 相當(dāng)易懂
 response = requests.post(url, json=data)

此時(shí)我們傳入的 data 字典對(duì)象會(huì)被轉(zhuǎn)換為 JSON 字符串放到 POST Body 體中,同時(shí) request headers 中會(huì)添加Content-Type: application/json

POST 請(qǐng)求常用的 Body 類型還有一種叫 multipart/form-data, 主要用來(lái)傳輸長(zhǎng)字節(jié)的類容(如文件),想了解更多的朋友看這:傳送門

PUT, DELETE, PATCH 請(qǐng)求

這幾個(gè)都是 POST 的多胞胎兄弟, requests 庫(kù)中把方法名改成對(duì)應(yīng)的名稱就好了,舉個(gè)栗子:

response = requests.put(url, json=data)

response 對(duì)象是什么東東

前面我們講了如何請(qǐng)求及請(qǐng)求的參數(shù)類型,那請(qǐng)求后我們?cè)趺蠢斫獾玫降?response 對(duì)象呢?

response 對(duì)象包含了請(qǐng)求信息和服務(wù)端返回的信息,其中常用到的屬性有以下:

response.text 得到解碼字符串,requests 根據(jù)響應(yīng)內(nèi)容自動(dòng)解碼

>response.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...'

response.content 得到字節(jié)類型響應(yīng)體內(nèi)容

>response.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

response.json() 如果響應(yīng)是 JSON 字符串,該方法得到反序列化對(duì)象,如列表、字典

>response.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

我想使用代理

爬蟲?你懂的

proxies = {
    "http": "http://127.0.0.1:8888",
    "https": "https://127.0.0.1:8888",
}
# 請(qǐng)求中設(shè)置代理(proxies)
response = requests.get("https://www.taobao.com", proxies=proxies)

The end, See you!

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容