本文實(shí)例講述了Python常用模塊之requests模塊用法。分享給大家供大家參考,具體如下:
一. GET請(qǐng)求
1.訪問一個(gè)頁面
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)
詳細(xì)用法:
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

