Python常用模块之requests模块用法分析
本文实例讲述了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程序设计有所帮助。
相关文章
Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
这篇文章主要介绍了Python3实现将文件归档到zip文件及从zip文件中读取数据的方法,涉及Python针对zip文件操作的相关技巧,需要的朋友可以参考下2015-05-05
Jupyter Notebook出现不是内部或外部的命令解决方案
这篇文章主要介绍了Jupyter Notebook出现不是内部或外部的命令解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-06-06
Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算
今天小编就为大家分享一篇关于Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-12-12
Python Asyncio库之asyncio.task常用函数详解
Asyncio在经过一段时间的发展以及获取Curio等第三方库的经验来提供更多的功能,目前高级功能也基本完善。本文主要介绍了Asyncio库中asyncio.task常用函数的使用,需要的可以参考一下2023-03-03


最新评论