当前位置:网站首页>请求模块(requests)
请求模块(requests)
2022-07-01 06:17:00 【HHYZBC】
requests模块是python中常用的发送请求模块,作用是发送http请求,获取响应数据。使用前需要使用pip进行下载。
pip install requests
使用requests
- 使用requests发送get请求
requests.get('https://www.douban.com/')
- 发送带有参数的get请求
传入一个字典作为params参数即可
requests.get('https://www.douban.com/',params={'a':'python','b':'100'})
实例请求的url则为:
https://www.douban.com/search?a=python&b=100'
- 发送需要传入HTTP Header的get请求
传入一个字段作为headers参数即可
requests.get('https://www.douban.com/', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})
- 发送json数据的get请求:
传入一个字典作为json参数即可
params = {'key': 'value'}
requests.post(url, json=params)
- 发送文件的get请求
传入一个字典作为files参数即可,字典的值为传入的文件,读取文件时需要使用rb模式二进制进行读取。
upload_files = {'file': open('report.xls', 'rb')}
requests.post(url, files=upload_files)
- 发送带有Cookie的get请求
传入一个字典作为cookies参数即可。
cookies= {'name': 'hhh', 'pwd': 'working'}
requests.get(url, cookies=cookies)
- 指定超时时间的get请求
传入一个数字作为timeout参数即可。注意timeout参数的单位是秒
requests.get(url, timeout=3)
- 发送post请求
将get()方法改成post()方法即可。如何传入一个字典作为data参数,表示作为post请求的数据。
requests.post('https://accounts.douban.com/login', data={'form_email': '[email protected]'})
如果需要使用其他请求方法时,则将post()方法换成响应的方法即可。如put(),delete()等,就可以使用put或者
delete方式请求数据了。
响应内容常用方法
使用上面方法后,都会返回一个对象,包含着所有响应内容。一般使用response进行接收,表示响应内容。该对象常用的属性有:
- status_code
- 状态码
- text
- 响应体
- content
- 也是响应体,但是无论响应是文本还是二进制内容,
content
属性获得都是bytes
对象 - 可以在该属性后面再使用decode方法,对数据进行解码操作,默认是utf-8
- 也是响应体,但是无论响应是文本还是二进制内容,
- encoding
- 查看编码格式
- json
- 获取json数据
- headers
- 获取响应头
- Cookie
- 获取Cookie
边栏推荐
- Dongle data collection
- 【ManageEngine卓豪】助力黄石爱康医院实现智能批量化网络设备配置管理
- HDU - 1501 zipper (memory deep search)
- Minio error correction code, construction and startup of distributed Minio cluster
- c# Xml帮助类
- Restframework-simplejwt rewrite authentication mechanism
- Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
- How does MySQL store Emoji?
- Forkjoin and stream flow test
- srpingboot security demo
猜你喜欢
Top 10 Free 3D modeling software for beginners in 2022
【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全
One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is
High order binary search tree
ManageEngine卓豪助您符合ISO 20000标准(四)
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
Excel visualization
图片服务器项目测试
指数法和Random Forest实现山东省丰水期地表水体信息
MongoDB:一、MongoDB是什么?MongoDB的优缺点
随机推荐
Make: g++: command not found
HCM Beginner (I) - Introduction
Pychart configuring jupyter
10-golang运算符
What if the data in the cloud disk is harmonious?
[summary of problem thinking] Why is the register reset performed in user mode?
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
【ManageEngine卓豪】局域网监控的作用
DEV XPO对比之UOW
kubeadm搭建kubenetes 集群(个人学习版)
端口扫描工具对企业有什么帮助?
IT服务管理(ITSM)在高等教育领域的应用
分布式锁实现
高阶-二叉平衡树
让田头村变甜头村的特色农产品是仙景芋还是白菜
Golang panic recover custom exception handling
【LeetCode】Day91-存在重复元素
SystemVerilog learning-07-class inheritance and package use
【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
Index method and random forest to realize the information of surface water body in wet season in Shandong Province