当前位置:网站首页>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)
边栏推荐
- Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales
- MySQL utilise la vue pour signaler les erreurs, Explicit / show ne peut pas être publié; Verrouillage des fichiers privés pour la table sous - jacente
- Pesticide synergist - current market situation and future development trend
- MPLS③
- Hbuilder link Xiaoyao simulator
- [turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
- Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
- When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
- [software implementation series] software implementation interview questions with SQL joint query diagram
- Méthode de calcul de la connexion MSSQL de la carte esp32c3
猜你喜欢

Life cycle of instance variables, static variables and local variables

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

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

Huawei cloud micro certification Huawei cloud computing service practice has been stable

String & memory function (detailed explanation)

Push technology practice | master these two tuning skills to speed up tidb performance a thousand times!

17. File i/o buffer

MPLS③

Openbionics robot project introduction | bciduino community finishing

Learn these super practical Google browser skills, girls casually flirt
随机推荐
Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
Special copy UML notes
Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.
Openbionics exoskeleton project introduction | bciduino community finishing
Neo4j learning notes
I don't know why it can't run in the project and how to change it
A. ABC
Huawei cloud micro certification Huawei cloud computing service practice has been stable
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
Mysql-15 aggregate function
Containerization technology stack
Pyinstaller packaging py script warning:lib not found and other related issues
Keep an IT training diary 054- opening and closing
Trading software programming
MySQL statement learning record
Setting function of Jerry's watch management device [chapter]
Solution of cursor thickening
Portable two-way radio equipment - current market situation and future development trend
It's corrected. There's one missing < /script >, why doesn't the following template come out?