当前位置:网站首页>Usage of requests module
Usage of requests module
2022-07-03 07:36:00 【start field】
python The libraries that initiate requests for web pages in urllib and requests,Python Reptilian time , More recommended requests library . because requests Than urllib More convenient . that request The function of the library is to simulate the browser to send a request to the web page .
How to use it ? Can be divided into 5 Step .
1、 Appoint url
2、 Send a request
3、 Get response data
4、 Parsing data
5、 Save the data
Programming begins
First, in the pycharm Download requests library , In the file -> Set up -> project find python Interpreter
Click on the top left + Number , Input requests, Then install .
Build a new project , Create a new one python file , Enter... On the first line # -- coding:UTF-8 --
This is a python Document coding statement , To illustrate your Python The source program file is encoded with the code used . By default, your program needs to use ascii Code to write , But if you write Chinese in it ,python The interpreter usually reports an error , But if you add the file code you use ,python It will be processed automatically and no error will be reported .
Simply put, you can write Chinese without reporting errors . Then import requests library .
import requests
After importing the library, you can divide it according to the above 5 Step start programming , Here is a simple way to get Sogou Page source code to introduce .
# -- coding:UTF-8 --
import requests
if __name__ == "__main__":
#1、 designated url
url = 'https://www.sogou.com/'
#2、 Send a request
#get Method will return a response object
response = requests.get(url=url)
#3、 Get response data .text What is returned is the response data in the form of string
html = response.text
#4、 Data analysis
print(html)
#5、 Save the data
with open('./sogou.html','w',encoding="utf-8") as fp:
fp.write(html)
print(' End of crawling data ')
The second step can be get Mode request , It can also be post Mode request .
The third step requests After sending the request , The server will return a response , and requests Usually, the response content is automatically decoded .
response.text Get the response content in the form of text
requests.content Get the response content in binary form
requests.json() Is to obtain json The way of form
requests.raw You can get the original data stream , use .read() You can read the contents
The fourth step is to simply print the data , If you want some data in the web page, you can use xpath、beautifulsoup、 Regular expressions are filtered out .
The fifth step is to simply save Sogou Source data .
requests Library has many other uses , You can have a look at requests Official document of the library
Document address :https://docs.python-requests.org/zh_CN/latest/
边栏推荐
- [mindspire paper presentation] summary of training skills in AAAI long tail problem
- The babbage industrial policy forum
- Understanding of class
- Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
- Segment read
- 论文学习——鄱阳湖星子站水位时间序列相似度研究
- Vertx's responsive redis client
- 技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
- Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
- 你开发数据API最快多长时间?我1分钟就足够了
猜你喜欢
Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
VMware network mode - bridge, host only, NAT network
你开发数据API最快多长时间?我1分钟就足够了
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
FileInputStream and fileoutputstream
【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
Es writing fragment process
URL programming
Arduino Serial系列函数 有关print read 的总结
随机推荐
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
最全SQL与NoSQL优缺点对比
docket
Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Chapter VI - Containers
Custom generic structure
Segment read
技术干货|关于AI Architecture未来的一些思考
技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
pgAdmin 4 v6.11 发布,PostgreSQL 开源图形化管理工具
Lombok cooperates with @slf4j and logback to realize logging
IndexSort
An overview of IfM Engage
Es writing fragment process
2021-07-18
Epoll related references
Lucene merge document order
【LeetCode】2. Valid Parentheses·有效的括号