使用Python爬蟲庫requests傳送請求、傳遞URL引數、定製headers
阿新 • • 發佈:2020-01-31
首先我們先引入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的使用方法請點選下面的相關連結