程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python crawler (II)

編輯:Python

Python Reptiles ( Two )

One 、 Request object customization —User-Agent Anti climbing mechanism

Request object customization : because urlopen There is no dictionary type data store in the method , therefore headers It cannot be stored directly
Custom of request object Purpose : Is to solve the first means of anti climbing User-Agent.

import urllib.request
url = 'https://cn.bing.com/search?q=CSDN'
# url The composition of 
# http/https --> agreement 
# www.baidu.com --> host 
# Port number --> http : 80 https : 443 mysql : 3306
# route --> search
# Parameters --> q=CSDN (? after )
# Anchor point --> #
headers = {

'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44'
}
# Request object customization 
# because urlopen There is no dictionary type data store in the method , therefore headers It cannot be stored directly 
request = urllib.request.Request(url = url , headers = headers) # In this case, you need to specify the parameter name , If it is not specified, one-to-one correspondence between arguments and formal parameters cannot be achieved 
response = urllib.request.urlopen(request)
content = response.read().decode('utf8')
print(content)

Two 、get Requested quote Method
take ‘ Jay Chou ’ convert to unicode Encoding format --> Depend on urllib.parse

import urllib.parse
# take Jay Chou convert to unicode Encoding format --> Depend on urllib.parse
name = urllib.parse.quote(' Jay Chou ')
print(name)
import urllib.request
import urllib.parse
url = 'https://www.baidu.com/s?wd=' # https://www.baidu.com/s?wd=%E5%91%A8%E6%9D%B0%E4%BC%A6
headers = {

'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44'
}
# take Jay Chou convert to unicode Encoding format --> Depend on urllib.parse
name = urllib.parse.quote(' Jay Chou ')
print(name)
url = url + name
print(url)
# Request object customization 
request = urllib.request.Request(url= url ,headers = headers)
response = urllib.request.urlopen(request)
content = response.read().decode()
print(content)

3、 ... and 、get Requested urlencode Method

import urllib.parse
data = {

'wd': ' Jay Chou ',
'sex':' male ',
'location':' Taiwan Province of China '
}
a = urllib.parse.urlencode(data)
print(a)


step :
1. Get the requested resource path
2. Request object customization
3. Simulate the browser to send a request to the server
4. Get web source data
5. Output

import urllib.parse
import urllib.request
base_url = 'https://www.baidu.com/s?'
data = {

'wd': ' Jay Chou ',
'sex':' male ',
'location':' Taiwan Province of China '
}
new_url = urllib.parse.urlencode(data)
# Get the requested resource path 
url = base_url + new_url
headers = {

'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44'
}
# Request object customization 
request = urllib.request.Request(url = url , headers = headers)
# Simulate the browser to send a request to the server 
response = urllib.request.urlopen(request)
# Get web source data 
content = response.read().decode('utf-8')
print(content)

Four 、post Request Baidu to translate the case

# post request 
import urllib.request
import urllib.parse
url = 'https://fanyi.baidu.com/sug'
headers = {

'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44'
}
data = {

'kw' : 'spider'
}
# post Requested parameters Must be coded 
data = urllib.parse.urlencode(data).encode('utf-8')
# post The requested parameters are not spliced in url Back , Instead, it needs to be placed in the customized parameters of the request object 
# post The requested parameters must be encoded 
request = urllib.request.Request(url= url ,data=data,headers=headers)
# Simulate the browser to send a request to the server 
response = urllib.request.urlopen(request)
# Get the data of the response 
content = response.read().decode('utf-8')
print(content)
# post The parameters of the request mode must be encoded data = urllib.parse.urlencode(data)
# After coding , Must call encode() Method data = urllib.parse.urlencode(data).encode('utf-8')
# Parameters are placed in the method customized by the request object request = urllib.request.Request(url= url ,data=data,headers=headers)
# Change the string to json object 
import json
obj = json.loads(content)
print(obj)
{
"errno":0,"data":[{
"k":"spider","v":"n. \u8718\u86db; \u661f\u5f62\u8f6e\uff0c\u5341\u5b57\u53c9; \u5e26\u67c4\u4e09\u811a\u5e73\u5e95\u9505; \u4e09\u811a\u67b6"},{
"k":"Spider","v":"[\u7535\u5f71]\u8718\u86db"},{
"k":"SPIDER","v":"abbr. SEMATECH process induced damage effect revea"},{
"k":"spiders","v":"n. \u8718\u86db( spider\u7684\u540d\u8bcd\u590d\u6570 )"},{
"k":"spidery","v":"adj. \u50cf\u8718\u86db\u817f\u4e00\u822c\u7ec6\u957f\u7684; \u8c61\u8718\u86db\u7f51\u7684\uff0c\u5341\u5206\u7cbe\u81f4\u7684"}]}
{
'errno': 0, 'data': [{
'k': 'spider', 'v': 'n. spider ; Starwheel , Cross fork ; Three legged pan with handle ; The tripod '}, {
'k': 'Spider', 'v': '[ The movie ] spider '}, {
'k': 'SPIDER', 'v': 'abbr. SEMATECH process induced damage effect revea'}, {
'k': 'spiders', 'v': 'n. spider ( spider The plural of a noun )'}, {
'k': 'spidery', 'v': 'adj. Slender like spider legs ; Cobweb like , Very delicate '}]}

5、 ... and 、post Request Baidu to translate the case in detail

# post Request Baidu to translate in detail 
import urllib.request
import urllib.parse
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
headers = {

# 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44'
# 'Accept':' */*',
# # 'Accept-Encoding':' gzip, deflate, br',
# 'Accept-Language':' zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
# 'Acs-Token':' 1655794810538_1655870963719_doKLwiFB8fBU4raSo3DyVeE1j9Kcfar9eZ7/KCCkYi/xVTDeTcb5b1ZVsJLMLyWCRoSk7D9QXGzMwPMrG3k9/KF+lripnq/IT/lKZRFtsx2mgqAQFfmEOVpOyCMUk/ysqeKlDRLQxm9QGT+ZmbMxC8YUGkIvL2BYvUlZiUToEScm+WalBblg8MnDH6ZQdV4xk5vDhekXq2xi/5qMufx5qP0PAghdX4ubcP9wlHGUuJdRgkw+14HDZLZTNuiVAU4iP+qn59DPLkMrtA7dbX+0mgm2nU6WbWNyZDgSVGFkv1jv/09OOEiCngeNxqkpRMVrDEQY2PusrEb0aJxuyf7Ly5xbZlXqoYHAJ+cv2UYoItE=',
# 'Connection':' keep-alive',
# 'Content-Length':' 135',
# 'Content-Type':' application/x-www-form-urlencoded; charset=UTF-8',
'Cookie: BAIDUID_BFESS=4AE41A61F199E97DFDEC24D9C529DC45:FG=1; BIDUPSID=40188254DB80FE4D23CEB3A62ECAE855; PSTM=1655736944; BAIDUID=40188254DB80FE4D6469C246B7564749:FG=1; ZFY=7IyWFSZJ9Jg6dusQDydJfYyIgD9iZLLDKF:AcE24Fi94':'C; ariaDefaultTheme=undefined; RT="z=1&dm=baidu.com&si=d0sjqupvsj&ss=l4mvs3fg&sl=w&tt=evz&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=9vue&ul=9ysu&hd=9yw5"; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; delPer=0; PSINO=6; H_PS_PSSID=36553_36673_36454_31253_34813_36165_36569_36074_36520_26350_36467_36311; BA_HECTOR=212k80a00580ah2h801hb4vm914; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1655866475; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1655870954; ab_sr=1.0.1_ZjcyNDAwOWM1ZGNhNTQ5YTVlZjdkMmFkZDU1MjJlZDBjOThhYzQ3OWUwZmE1N2VhNmVhNjA4MDM4Mzg4MTVmNzA3YzZjNDUxNzI5MGM1ZTNlZWM0NTQ5MzhkYzdjZDExMjgxMmRmZjRlYTdjYjAwNWJhMTVhNmVjNmI1YTZjMmE3N2MzN2M1MGZkOWU3MDMxZTViZmNiMGJkYTNkMWNiZA==',
# 'Host':' fanyi.baidu.com',
# 'Origin: https':'//fanyi.baidu.com',
# 'Referer: https':'//fanyi.baidu.com/?aldtype=16047',
# 'sec-ch-ua':' " Not A;Brand";v="99", "Chromium";v="102", "Microsoft Edge";v="102"',
# 'sec-ch-ua-mobile':' ?0',
# 'sec-ch-ua-platform':' "Windows"',
# 'Sec-Fetch-Dest':' empty',
# 'Sec-Fetch-Mode':' cors',
# 'Sec-Fetch-Site':' same-origin',
# 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44',
# 'X-Requested-With':' XMLHttpRequest',
}
data = {

'from' : 'en',
'to': 'zh',
'query': 'love',
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': '198772.518981',
'token': 'f49e30e81d08ca57c8404ba16285a065',
'domain': 'common',
}
# post Requested parameters Must be coded 
data = urllib.parse.urlencode(data).encode('utf-8')
# Request object customization 
request = urllib.request.Request(url=url,data=data,headers=headers)
# Simulate the browser to send a request to the server 
response = urllib.request.urlopen(request)
# Get the data of the response 
content = response.read().decode('utf-8')
import json
obj = json.loads(content)
print(obj)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved