之前爬取某数据统计平台时遇到了相当严重的反爬机制,采用普通的Selenium也无法绕过。之前尝试过undetected_chromedriver可以使用,但无法设置无头模式,使用起来还是有一定的不美观性。正好近日学习了Drission Page这款相当高效的工具,顺手掏出这个项目重构了一下。填输入数据相当简单:
import os
import cv2
import time
import random
from datetime import datetime
from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions
input_code = ""
# 创建页面对象
co = ChromiumOptions().auto_port() # 指定程序每次使用空闲的端口和临时用户文件夹创建浏览器
co.set_argument('--no-sandbox') # 无沙盒模式
co.set_argument('--headless=new') # 无界面系统添加
co.set_argument('--disable-gpu') # 禁用gpu,提高加载速度
co.set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
# 创建浏览器窗口
webpage = ChromiumPage(co)
url = ""
webpage.get(url)
webpage.wait(5)
# 选择出口
out_span = webpage.ele('@text()=出口')
out_input = out_span.next('tag:input')
out_input.click()
# 选择币种
currency_span = webpage.ele('@text()=美元')
currency_input = currency_span.next('tag:input')
currency_input.click()
# 选择分月输出
by_month_input = webpage.ele('@name=monthFlag')
by_month_input.click()
# 起止年月
start_select = webpage.ele('@id=startMonth')
end_select = webpage.ele('@id=endMonth')
if start_select.child(-2):
start_month_option = start_select.child(-2)
else:
start_month_option = start_select.child(-1)
end_month_option = end_select.child(-1)
start_month_value = start_month_option.attr('value')
end_month_value = end_month_option.attr('value')
start_select.select.by_option(start_month_option)
end_select.select.by_option(start_month_option)
output_field_select_1 = webpage.ele('@id=outerField1')
output_field_select_2 = webpage.ele('@id=outerField2')
output_field_select_1.select.by_value("CODE_TS")
output_field_select_2.select.by_value("ORIGIN_COUNTRY")
output_field_input_1 = webpage.ele("@id=outerValue1")
output_field_input_1.input("85434000,85439090,24041100,24041200,24041990")
do_search_button = webpage.ele('@@tag()=a@@id=doSearch', timeout=2)
do_search_button.click()
time.sleep(1)
# webpage.get_screenshot('test_dp.png')
try:
webpage.ele('xpath://div[@class="layui-layer-btn"]/a[@class="layui-layer-btn0"]', timeout=2).click()
except:
pass
time.sleep(1)
这里进入到滑块验证界面,也算是一个难点:
参考了网上的一些方法,通过将缺口图片和背景图用cv2进行一些处理来获取滑动量。但是最后结果并不准确,研究后发现是因为缺口小图并不准确。以另一幅图为例,获取到的小图和背景分别是:
可以发现直接通过get_screenshot()的方法获取到的小图并不是真的小图。暂时没发现准确获取小图的方法。转换了一下思路,只需要在背景图里找出面积较大的白色区域,就是缺口了。(滑动轨迹模拟的代码源于网络)
def find_blank_region(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 500]
largest_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(largest_contour)
return x
# 滑动轨迹模拟
def generate_track(distance):
def __ease_out_expo(step):
return 1 if step == 1 else 1 - pow(2, -10 * step)
tracks = [[random.randint(20, 60), random.randint(10, 40), 0]]
count = 30 + int(distance / 2)
_x, _y = 0, 0
for item in range(count):
x = round(__ease_out_expo(item / count) * distance)
t = random.randint(10, 20)
if x == _x:
continue
tracks.append([x - _x, _y, t])
_x = x
tracks.append([0, 0, random.randint(200, 300)])
times = sum([track[2] for track in tracks])
return tracks, times
然后运用这两个函数拖动滑块完成验证。这里虽然设置了一直循环,但是大部分情况只需要一次就够了。
while True:
background_canvas = webpage.ele('xpath://div[@id="captcha"]/canvas', timeout=2)
background_canvas.get_screenshot('captcha.png')
background_pic = cv2.imread('captcha.png')
x_num = find_blank_region(background_pic) * 1.08
print(x_num)
element = webpage.ele('xpath://div[@class="sliderMask"]/div[@class="slider"]')
element.drag(offset_x=x_num/2, duration=1)
# webpage.get_screenshot('test_move.png')
msg = webpage.ele('xpath://div[@id="msg"]', timeout=2).text
print(msg)
if msg == '验证通过!':
time.sleep(2)
webpage.ele('xpath://div[@class="page-container"]//a[@id="doSearch"]', timeout=2).click()
break
time.sleep(3)
最后下载csv:
time.sleep(random.randint(6,8))
download_button = webpage.ele('@@tag()=span@@text()=导出数据')
download_button.click()
confirm_download_button = webpage.ele('@@tag()=a@@class=layui-layer-btn0')
mission = confirm_download_button.click.to_download(r'D:/', 'test2.csv')
mission.wait()
# webpage.get_screenshot('test_dp2.png')
标签:DrissionPage,验证,webpage,cv2,ele,input,滑动,month,select
From: https://blog.csdn.net/m0_55893050/article/details/142201848