当前位置:网站首页>请求模块(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
边栏推荐
猜你喜欢

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

【ITSM】什么是ITSM,IT部门为什么需要ITSM

ArcServer密码重置(账号不可以重置)

SystemVerilog learning-08-random constraints and thread control

JMM详解

Understanding of C manualresetevent class
![[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map](/img/c0/299a406efea51f24b1701b66adc1e3.png)
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map

lxml模块(数据提取)

Teach you how to implement a deep learning framework
随机推荐
图片服务器项目测试
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
JDBC connection pool
Golang panic recover custom exception handling
SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
Flink实战--多流合并
Kubedm builds kubenetes cluster (Personal Learning version)
10-golang运算符
B-tree series
Multi label lsml for essay learning records
FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview
ArcServer密码重置(账号不可以重置)
Understanding of C manualresetevent class
Pol8901 LVDS to Mipi DSI supports rotating image processing chip
webapck打包原理--启动过程分析
高阶-二叉平衡树
golang panic recover自定义异常处理
【ManageEngine】如何实现网络自动化运维
【自动化运维】自动化运维平台有什么用
【KV260】利用XADC生成芯片温度曲线图