当前位置:网站首页>请求模块(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
边栏推荐
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
- three. JS summary
- 浏览器端保存数据到本地文件
- 让田头村变甜头村的特色农产品是仙景芋还是白菜
- 【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
- Thoughts on a "01 knapsack problem" expansion problem
- 【KV260】利用XADC生成芯片温度曲线图
- Pol8901 LVDS to Mipi DSI supports rotating image processing chip
- JDBC connection pool
猜你喜欢

【文件系统】如何在ubi之上运行squashfs

Picture server project test

Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)

High order binary balanced tree

DHT11 温湿度传感器

Infinite horizontal marble game

C# ManualResetEvent 类的理解

SystemVerilog learning-10-validation quantification and coverage

68 Cesium代码datasource加载czml

Linux closes the redis process SYSTEMd+
随机推荐
高阶-二叉搜索树详解
Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
libpng12.so. 0: cannot open shared object file: no such file or directory
连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
C# ManualResetEvent 类的理解
1034 Head of a Gang
利用百度地图查询全国地铁线路
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
可动的机械挂钟
69 Cesium代码datasource加载geojson
【ManageEngine】终端管理系统,助力华盛证券数字化转型
地宫取宝(记忆化深搜)
SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
What if the data in the cloud disk is harmonious?
68 cesium code datasource loading czml
JDBC database operation
Pychart configuring jupyter
Stack Title: parsing Boolean expressions
Tidb database characteristics summary