您好,登錄后才能下訂單哦!
首先我們先引入requests模塊
import requests
一、發送請求
r = requests.get('https://api.github.com/events') # GET請求 r = requests.post('http://httpbin.org/post', data = {'key':'value'}) # POST請求 r = requests.put('http://httpbin.org/put', data = {'key':'value'}) # PUT請求 r = requests.delete('http://httpbin.org/delete') # DELETE請求 r = requests.head('http://httpbin.org/get') # HEAD請求 r = requests.options('http://httpbin.org/get') # OPTIONS請求 type(r)
requests.models.Response
二、傳遞URL參數
URL傳遞參數的形式為:httpbin.org/get?key=val。但是手動的構造很麻煩,這是可以使用params參數來方便的構造帶參數URL。
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get("http://httpbin.org/get", params=payload) print(r.url)
http://httpbin.org/get?key1=value1&key2=value2
同一個key可以有多個value
payload = {'key1': 'value1', 'key2': ['value2', 'value3']} r = requests.get('http://httpbin.org/get', params=payload) print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3
三、定制headers
只需要將一個dict傳遞給headers參數便可以定制headers
url = 'https://api.github.com/some/endpoint' headers = {'user-agent': 'my-app/0.0.1'} r = requests.get(url, headers=headers)
更多關于Python爬蟲庫requests的使用方法請點擊下面的相關鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。