Python常用模塊之requests模塊用法分析
本文實例講述了Python常用模塊之requests模塊用法。分享給大家供大家參考,具體如下:
一. GET請求
1.訪問一個頁面
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.返回數(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.請求頭
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相關內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結》、《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python3實現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法
這篇文章主要介紹了Python3實現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法,涉及Python針對zip文件操作的相關技巧,需要的朋友可以參考下2015-05-05
Jupyter?Notebook出現(xiàn)不是內(nèi)部或外部的命令解決方案
這篇文章主要介紹了Jupyter?Notebook出現(xiàn)不是內(nèi)部或外部的命令解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python計算庫numpy進行方差/標準方差/樣本標準方差/協(xié)方差的計算
今天小編就為大家分享一篇關于Python計算庫numpy進行方差/標準方差/樣本標準方差/協(xié)方差的計算,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Python?Asyncio庫之a(chǎn)syncio.task常用函數(shù)詳解
Asyncio在經(jīng)過一段時間的發(fā)展以及獲取Curio等第三方庫的經(jīng)驗來提供更多的功能,目前高級功能也基本完善。本文主要介紹了Asyncio庫中asyncio.task常用函數(shù)的使用,需要的可以參考一下2023-03-03

