当前位置:网站首页>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/
边栏推荐
- Longest common prefix and
- 项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
- Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
- JS monitors empty objects and empty references
- HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
- 技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
- An overview of IfM Engage
- 【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
- IndexSort
- Vertx's responsive redis client
猜你喜欢

Leetcode 198: house raiding
![[mindspire paper presentation] summary of training skills in AAAI long tail problem](/img/34/9c9ec1b94edeecd4a3e7f20fdd8356.png)
[mindspire paper presentation] summary of training skills in AAAI long tail problem

Lucene skip table

Common architectures of IO streams

Take you through the whole process and comprehensively understand the software accidents that belong to testing

技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer

Topic | synchronous asynchronous

Lucene introduces NFA

TCP cumulative acknowledgement and window value update

Custom generic structure
随机推荐
What did the DFS phase do
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
Beginners use Minio
Use of generics
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
截图工具Snipaste
c语言指针的概念
Es writing fragment process
技术干货|关于AI Architecture未来的一些思考
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
Vertx multi vertical shared data
HCIA notes
Download address collection of various versions of devaexpress
昇思MindSpore再升级,深度科学计算的极致创新
Custom generic structure
Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
Traversal in Lucene
C code production YUV420 planar format file
Understanding of class