Python常用模块之requests模块用法分析
人气:0本文实例讲述了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('http://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('http://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程序设计有所帮助。
您可能感兴趣的文章:
加载全部内容