一、装载好chromedriver之后,运行命令行 "chrome.exe"(路径) --remote-debugging-port=9222 打开谷歌浏览器。
二、使用selenium打开网页:
def opendriver(url, domainlist):
"""
打开网站
"""
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
time.sleep(5)
# driver.maximize_window()
# # 获取打开滑块验证码页面的元素
formdf = icp_query(driver, url, domainlist)
return formdf
三、获取输入框批量输入数据,并破解验证码:
使用 driver.find_element_by_xpath 获取element时,xpath可通过浏览器打开网站,使用检查功能,找到element所对应行,右键复制xpath。
def icp_query(driver, url, domainlist):
"""
破解验证码并批量查询数据
"""
formdf = pd.DataFrame()
for ind in range(len(domainlist)):
while True:
try:
input = driver.find_element_by_css_selector("input.el-input__inner")
# print(input)
# # 输入查询网站
input.clear()
time.sleep(0.5)
inputtext = domainlist[ind]
input.send_keys(inputtext)
while True:
try:
time.sleep(2)
getcheck = driver.find_element_by_xpath("/html/body/div[1]/div/header/div[3]/div/button")
# getcheck = driver.find_element_by_css_selector("button.el-button.el-button--primary")
ActionChains(driver).move_to_element(getcheck).perform() # 悬停鼠标
# # 点击进入滑块验证码页面
getcheck.click()
print("click button")
time.sleep(1.5)
# 保存图片
html = etree.HTML(driver.page_source)
print(html.xpath('/html/body/div/div/div/div/div/div//img/@src'))
imgbases = list(filter(None, html.xpath('/html/body/div/div/div/div/div/div//img/@src')))
big_base = imgbases[1].split('base64,')[-1]
print('bg: ' + big_base)
except Exception as e:
print(e)
if input.text=='' or input.text==None:
alertcheck = driver.find_element_by_xpath("/html/body/div[4]/ul/li[3]/input")
ActionChains(driver).move_to_element(alertcheck).perform()
alertcheck.click()
driver.refresh()
continue
if big_base != '':
save_picture('bg.jpg', base64.b64decode(big_base))
# 保存缺口图
small_base = imgbases[2].replace('data:text/javascript;base64,', '')
print('tp: ' + big_base)
save_picture('tp.png', base64.b64decode(small_base))
# 计算距离
distance = identify_gap('bg.jpg', 'tp.png')
# 滑动滑块
slide_block(driver, distance)
try:
formdict = getform(driver, inputtext)
print(formdict)
break
except Exception as e:
print(e)
else:
print('没保存成功,重试')
driver.refresh()
if ind==0:
formdf = pd.DataFrame([formdict])
print('add first line to dataframe')
else:
formdf = formdf.append(formdict, ignore_index=True)
print('append to dataframe')
break
except Exception as e:
print(e)
driver.get(url)
time.sleep(4)
if len(domainlist)>0:
return formdf
else:
return 'no query list!'
其中,弹出滑动验证码后,保存图片并计算图片左端到缺口的距离
def save_picture(file_name, avatar_bytes):
"""
保存图片
"""
with open(file_name, 'wb+') as f:
f.write(avatar_bytes)
f.close()
print('图片保存成功')
def identify_gap(bg, tp):
"""
bg: 背景图片
tp: 缺口图片
计算滑块距离
"""
target_rgb = cv.imread(bg)
target_gray = cv.cvtColor(target_rgb, cv.COLOR_BGR2GRAY)
template_rgb = cv.imread(tp, 0)
res = cv.matchTemplate(target_gray, template_rgb, cv.TM_CCOEFF_NORMED)
value = cv.minMaxLoc(res)
print(value)
imgx = value[3][0]
return imgx
之后 ,为模拟人工滑动效果,计算滑动轨迹,滑动滑块
def get_track(distance):
"""
计算滑块移动轨迹
"""
track=[]
current=0
mid=distance*3/4.5
t=random.randint(2,3)/4.5
v=0
while current
四、破解完成后跳转,获取表单数据(根据实际网站代码调整dict)
def getform(driver, inputtext):
"""
获取表单数据
"""
time.sleep(1)
formdata = {}
formdata['input'] = inputtext
heading = driver.find_element_by_xpath('/html/body/div[1]/div/section/div/div/div[1]/div/div[1]/div[2]/table')
try:
formdata['name'] = driver.find_element_by_xpath('/html/body/div[1]/div/section/div/div/div[1]/div/div[1]/div[3]/table/tbody/tr/td[2]/div').text
formdata['filingnum'] = driver.find_element_by_xpath('/html/body/div[1]/div/section/div/div/div[1]/div/div[1]/div[3]/table/tbody/tr/td[3]/div').text
formdata['checkdate'] = driver.find_element_by_xpath('/html/body/div[1]/div/section/div/div/div[1]/div/div[1]/div[3]/table/tbody/tr/td[4]/div').text
formdata['ifretrict'] = driver.find_element_by_xpath('/html/body/div[1]/div/section/div/div/div[1]/div/div[1]/div[3]/table/tbody/tr/td[5]/div/span').text
except Exception as e:
print('find no item!')
return formdata