新聞中心
本文教程操作環(huán)境:windows7系統(tǒng)、python 3.9.1,DELL G3電腦。

1、get請求的部分參數(shù)
(1) url(請求的url地址,必需 )
import requests url="http://www.baidu.com" resp=requests.get(url)#向url對應(yīng)的服務(wù)器發(fā)送相應(yīng)的get請求,獲得對應(yīng)的相應(yīng) 。
(2)headers參數(shù)(請求頭,可選)
import requests
url=r"https://www.baidu.com/s"
Headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
}
response=requests.get(url=url,headers=Headers)2、requests.get請求實例
任何時候進行了類似 requests.get() 的調(diào)用,你都在做兩件主要的事情。其一,你在構(gòu)建一個 Request對象, 該對象將被發(fā)送到某個服務(wù)器請求或查詢一些資源。其二,一旦 requests 得到一個從服務(wù)器返回的響應(yīng)就會產(chǎn)生一個 Response 對象。該響應(yīng)對象包含服務(wù)器返回的所有信息,也包含你原來創(chuàng)建的 Request 對象。如下是一個簡單的請求,從 Wikipedia 的服務(wù)器得到一些非常重要的信息:
>>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')如果想訪問服務(wù)器返回給我們的響應(yīng)頭部信息,可以這樣做:
>>> r.headers
{'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':
'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',
'connection': 'close', 'cache-control': 'private, s-maxage=0, max-age=0,
must-revalidate', 'date': 'Thu, 14 Jun 2012 12:59:39 GMT', 'content-type':
'text/html; charset=UTF-8', 'x-cache-lookup': 'HIT from cp1006.eqiad.wmnet:3128,
MISS from cp1010.eqiad.wmnet:80'}然而,如果想得到發(fā)送到服務(wù)器的請求的頭部,我們可以簡單地訪問該請求,然后是該請求的頭部:
>>> r.request.headers
{'Accept-Encoding': 'identity, deflate, compress, gzip',
'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}以上就是requests在python中發(fā)送請求的方法,大家對于基礎(chǔ)的get參數(shù)有所了解后,可以自己動手試著給服務(wù)器發(fā)送請求,看看能否獨立完成相關(guān)的操作。
網(wǎng)頁名稱:創(chuàng)新互聯(lián)Python教程:requests在python中如何發(fā)送請求
網(wǎng)站路徑:http://fisionsoft.com.cn/article/djgieji.html


咨詢
建站咨詢
