当前位置:网站首页>The requests module uses
The requests module uses
2022-07-04 02:08:00 【SongErrors】
requests Module USES
requests Introduce
python A module based on network request of Zhongyuan , Very powerful , Simple and convenient , Very efficient , The function is to simulate the browser to send a request .
requests Module coding process
- Appoint URL
- Send a request
- Obtain corresponding data
- Persistent storage
Environmental installation
pip install requests
Actual code
demand : Crawler Sogou homepage page data
import requests
# One 、 Appoint url
url = 'https://www.sogou.com/'
# Two 、 Send a request
# 3、 ... and 、 use get The request returns a response object
response = requests.get(url=url)
# Get response data
page_text = response.text
# Four 、 Persistent storage
with open('sougou.html', 'w', encoding='utf-8') as fp:
fp.write(page_text)
To consolidate in action
1、 Crawl the search result page corresponding to the specified term of the search dog (get request )
import requests
# UA camouflage : The corresponding User-Agent Encapsulated into a dictionary ( Request header ) in
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50'
}
url = 'https://www.sogou.com/web'
# Handle url Parameters carried : Encapsulated in a dictionary
kw = input(" Enter keywords \n")
param = {
'query': kw
}
# For the specified url The request initiated corresponds to url It carries parameters , And the parameters are processed in the request process
response = requests.get(url=url, params=param, headers=headers)
page_text = response.text
fileName = kw+'.html'
# Persistent storage
with open(fileName, 'w', encoding='utf-8') as fp:
fp.write(page_text)
2、 Crack Baidu translation (post request )
import json
import requests
# UA camouflage : The corresponding User-Agent Encapsulated in a dictionary
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50'
}
url = 'https://fanyi.baidu.com/sug'
kw = input(' Translation input :\n')
data = {
'kw': kw
}
response = requests.post(url=url, data=data, headers=headers)
# Get response data :json Method returns a json object ( If the response data type is json Type can only be used :Content-Type: application/json)
result = response.json()
# Output translation results
print(' Translation results :')
for word in result['data']:
print(word['k'], word['v'])
# Persistent storage
fp = open('{}.json'.format(kw), 'w', encoding='utf-8')
json.dump(result, fp=fp, ensure_ascii=False)
3、 Climb to the ranking list of Douban films
import json
from unittest import result
import requests
url = 'https://movie.douban.com/j/chart/top_list'
# UA camouflage : The corresponding User-Agent Encapsulated in a dictionary
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50'
}
param = {
'type': '24',
'interval_id': '100:90',
'action': '',
'start': '0',
'limit': '100'
}
response = requests.get(url=url, params=param, headers=headers)
result = response.json()
# Output results
for moive in result:
print(' ranking :{0}, The movie name :{1}'.format(moive['rank'], moive['title']))
# Persistent storage
fp = open('douban.json', 'w', encoding='utf-8')
json.dump(result, fp=fp, ensure_ascii=False)
边栏推荐
- Sword finger offer 14- I. cut rope
- Portable two-way radio equipment - current market situation and future development trend
- Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
- Override and virtual of classes in C #
- Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
- Conditional statements of shell programming
- Do you know the eight signs of a team becoming agile?
- Hbuilder link Xiaoyao simulator
- Development of user-defined navigation bar in uniapp
- Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
猜你喜欢
What is the intelligent monitoring system of sewage lifting pump station and does it play a big role
Pytoch residual network RESNET
The contact data on Jerry's management device supports reading and updating operations [articles]
Conditional statements of shell programming
Openbionics exoskeleton project introduction | bciduino community finishing
Chain ide -- the infrastructure of the metauniverse
Life cycle of instance variables, static variables and local variables
Hbuilder link Xiaoyao simulator
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
What are the advantages and disadvantages of data center agents?
随机推荐
13. Time conversion function
PMP daily three questions (February 14, 2022)
Introduction to graphics: graphic painting (I)
Magical usage of edge browser (highly recommended by program ape and student party)
G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
Containerization technology stack
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
[leetcode daily question] a single element in an ordered array
Idea if a class cannot be found, it will be red
Human resource management online assignment
What are the advantages and disadvantages of data center agents?
Three layer switching ①
Difference between value and placeholder
Reading notes - learn to write: what is writing?
2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
12. Gettimeofday() and time()
Sequence sorting of basic exercises of test questions
Douban scoring applet Part-3
Day05 branch and loop (II)