当前位置:网站首页>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)
边栏推荐
- Small program graduation project based on wechat e-book small program graduation project opening report function reference
- Introduction to Tianchi news recommendation: 4 Characteristic Engineering
- Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
- The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
- Sequence sorting of basic exercises of test questions
- Will the memory of ParticleSystem be affected by maxparticles
- Pyrethroid pesticide intermediates - market status and future development trend
- The reasons why QT fails to connect to the database and common solutions
- Experimental animal models - current market situation and future development trend
- FRP intranet penetration
猜你喜欢

Hbuilder link Xiaoyao simulator

Remember another interview trip to Ali, which ends on three sides

JVM performance tuning and practical basic theory - medium

Force buckle day32

The reasons why QT fails to connect to the database and common solutions

Take you to master the formatter of visual studio code

How programmers find girlfriends through blind dates

Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader

Final consistency of MESI cache in CPU -- why does CPU need cache

On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
随机推荐
SQL statement
2022 new examination questions for safety management personnel of hazardous chemical business units and certificate examination for safety management personnel of hazardous chemical business units
How programmers find girlfriends through blind dates
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Openbionics robot project introduction | bciduino community finishing
Should enterprises start building progressive web applications?
Basic editing specifications and variables of shell script
The contact data on Jerry's management device supports reading and updating operations [articles]
Jerry's update contact [article]
C learning notes: C foundation - Language & characteristics interpretation
Small program graduation project based on wechat e-book small program graduation project opening report function reference
MySQL advanced SQL statement (1)
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
Setting function of Jerry's watch management device [chapter]
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Cancer biopsy instruments and kits - market status and future development trends
Day05 branch and loop (II)
Bacteriostatic circle scanning correction template