本文實例講述了Python常用模塊之requests模塊用法。分享給大家供大家參考,具體如下:
一. GET請求
1.訪問一個頁面
import requests r=requests.get('http://www.so.com') print(r.status_code) print(r.text)
2.帶參數
import requests params = {'a':1,'b':2} r=requests.get('http://www.so.com', params=params) print(r.url)
3.返回數據顯示
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.請求頭
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請求
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.超時
r = requests.get(url, timeout=5)
詳細用法:
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python函數使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
