当前位置:网站首页>Requests module (requests)
Requests module (requests)
2022-07-01 06:27:00 【HHYZBC】
requests The module is python Sending request module commonly used in , The function is to send http request , Get response data . It needs to be used before use pip Download .
pip install requestsUse requests
- Use requests send out get request
requests.get('https://www.douban.com/')- Send... With parameters get request
Pass in a dictionary as params Parameters can be
requests.get('https://www.douban.com/',params={'a':'python','b':'100'})Instance requested url Then for :
https://www.douban.com/search?a=python&b=100'- Sending requires incoming HTTP Header Of get request
Pass in a field as headers Parameters can be
requests.get('https://www.douban.com/', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})- send out json Data get request :
Pass in a dictionary as json Parameters can be
params = {'key': 'value'}
requests.post(url, json=params)- Send the file get request
Pass in a dictionary as files Parameters can be , The value of the dictionary is the incoming file , When reading files, you need to use rb Mode binary for reading .
upload_files = {'file': open('report.xls', 'rb')}
requests.post(url, files=upload_files)
- Send with Cookie Of get request
Pass in a dictionary as cookies Parameters can be .
cookies= {'name': 'hhh', 'pwd': 'working'}
requests.get(url, cookies=cookies)- Specify the timeout time get request
Pass in a number as timeout Parameters can be . Be careful timeout The unit of parameter is seconds
requests.get(url, timeout=3)- send out post request
take get() Method changed to post() The method can . How to import a dictionary as data Parameters , To act as post Requested data .
requests.post('https://accounts.douban.com/login', data={'form_email': '[email protected]'}) If you need to use other request methods , Will post() Method can be changed into a response method . Such as put(),delete() etc. , You can use put perhaps delete Method to request data .
Common methods for responding to content
After using the above method , Will return an object , It contains all the response contents . In general use response Receive , Response content . The common attributes of this object are :
- status_code
- Status code
- text
- Response body
- content
- Is also a response body , But whether the response is text or binary content ,
contentAttribute acquisition isbytesobject - You can use this property later decode Method , Decode the data , The default is utf-8
- Is also a response body , But whether the response is text or binary content ,
- encoding
- View encoding format
- json
- obtain json data
- headers
- Get response header
- Cookie
- obtain Cookie
边栏推荐
猜你喜欢

浅谈SIEM

C语言课设学生信息管理系统(大作业)

【企业数据安全】升级备份策略 保障企业数据安全

Uniapp tree level selector

C语言课设物业费管理系统(大作业)

Understanding of C manualresetevent class

How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?

Design of sales management system for C language course (big homework)

分布式锁实现

Tidb database characteristics summary
随机推荐
C语言课设图书信息管理系统(大作业)
Minio error correction code, construction and startup of distributed Minio cluster
Solve the problem of garbled files uploaded by Kirin v10
Three minutes to quickly understand the whole process of website development
【Unity Shader 描边效果_案例分享第一篇】
make: g++:命令未找到
[summary of knowledge points] chi square distribution, t distribution, F distribution
记磁盘扇区损坏导致的Mysql故障排查
C语言课设物业费管理系统(大作业)
Diffusion (multi-source search)
SystemVerilog learning-06-class encapsulation
pycharm 配置jupyter
JMM details
网络爬虫
[unity shader ablation effect _ case sharing]
C language course set up property fee management system (big work)
golang panic recover自定义异常处理
To sort out the anomaly detection methods, just read this article!
浅谈SIEM
Uniapp tree level selector