如何在Python中使用requests模塊?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
一. GET請(qǐng)求
1.訪問(wèn)一個(gè)頁(yè)面
import requests r=requests.get('http://www.so.com') print(r.status_code) print(r.text)
2.帶參數(shù)
import requests params = {'a':1,'b':2} r=requests.get('http://www.so.com', params=params) print(r.url)
3.返回?cái)?shù)據(jù)顯示
import requests r = requests.get('https://pullwave.com/pw2/api/acc_query_words?auth_usr=free_vip&src=s0&w1=%E6%8A%96%E9%9F%B3&w2=&date_end=2019-4-6&json=1') print(r.content) print(r.text) print(r.json()) print(r.headers)
4.請(qǐng)求頭
import requests r = requests.get('https://pullwave.com/pw2/api/acc_query_words?auth_usr=free_vip&src=s0&w1=%E6%8A%96%E9%9F%B3&w2=&date_end=2019-4-6&json=1', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'}) print(r.content) print(r.text) print(r.json())
二.POST請(qǐng)求
1.傳參
r = requests.post('http://www.so.com', data={'fdsafdfs': 'fsdsfa', 'fdsfs': 'dfsfs'})
2.傳json
params = {'key': 'value'} r = requests.post(url, json=params)
3.傳文件
upload_files = {'file': open('234.txt', 'rb')} r = requests.post(url, files=upload_files)
4.帶cookie
url = 'http://www.so.com' cs = {'lalala': 'lalala', 'lallala': '23232'} r = requests.get(url, cookies=cs)
5.超時(shí)
r = requests.get(url, timeout=5)
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享標(biāo)題:如何在Python中使用requests模塊-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.2m8n56k.cn/article28/esecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、網(wǎng)站策劃、網(wǎng)站改版、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容