欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python爬蟲庫requests發(fā)送請求、傳遞URL參數(shù)、定制headers

 更新時間:2020年01月25日 11:50:39   作者:BQW_  
今天為大家介紹一下Python爬蟲庫requests的發(fā)送請求、傳遞URL參數(shù)、定制headers的基礎(chǔ)使用方法

首先我們先引入requests模塊

import requests

一、發(fā)送請求

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參數(shù)

URL傳遞參數(shù)的形式為:httpbin.org/get?key=val。但是手動的構(gòu)造很麻煩,這是可以使用params參數(shù)來方便的構(gòu)造帶參數(shù)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參數(shù)便可以定制headers

url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

更多關(guān)于Python爬蟲庫requests的使用方法請點擊下面的相關(guān)鏈接

相關(guān)文章

最新評論