当前位置:网站首页>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/
边栏推荐
- opensips与对方tls sip trunk对接注意事项
- Beginners use Minio
- IPv4 address
- Es writing fragment process
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- Vertx's responsive MySQL template
- Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
- Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
- Understanding of class
- Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
猜你喜欢

项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass

Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition

Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task

VMWare网络模式-桥接,Host-Only,NAT网络

Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition

FileInputStream and fileoutputstream

【MySQL 12】MySQL 8.0.18 重新初始化

Leetcode 198: house raiding

Summary of Arduino serial functions related to print read

Custom generic structure
随机推荐
The underlying mechanism of advertising on websites
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
Segment read
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
JS monitors empty objects and empty references
Longest common prefix and
Epoll related references
Download address collection of various versions of devaexpress
Arduino 软串口通信 的几点体会
Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
Custom generic structure
VMware virtual machine installation
Lucene skip table
Mail sending of vertx
Comparison of advantages and disadvantages between most complete SQL and NoSQL
Lombok -- simplify code
Hnsw introduction and some reference articles in lucene9
The babbage industrial policy forum