最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python用requests如何实现http请求 python用requests实现http请求代码实例
时间:2019-10-31 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python用requests实现http请求代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1. get
import requests # 最简单的get请求 r = requests.get(url) print(r.status_code) print(r.json()) # url 中?key=value&key=value r = requests.get(url, params=params) # form 表单 params = {"username":"name", "password":"passw0rd"} headers = {'Content-Type':'application/x-www-form-urlencoded'} r = requests.get(url, params=params, headers=headers) # 下载 r = requests.get(url) r.raise_for_status() with open(target, 'wb') as f: for ch in r.iter_content(10000): result_file_size += f.write(ch)
2. post请求
data = {'name':'train', 'device':'CN0989'} r = requests.post(url, json=data) #上传 files = { "file": (os.path.basename(filepath), open(filepath, "rb"), "application/zip") } print('POST %s'%url) with open(filepath, 'rb') as f: r = requests.post(url, files=files)
3. 登录
_session = requests.Session() # login url = '%s/login'%_basic_url params = {"username":"admin", "password":"admin"} headers = {'Content-Type':'application/x-www-form-urlencoded'} r = _session.post(url, params=params, headers=headers) #做其他请求 r = _session.get(url) _session.close()
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27