登录有许多的反扒机制,首先是登录不可以快速请求我用selenium接管打开的浏览器绕过
第二是图形滑块验证码,使用图像层次进行绕过
在开始运行代码之前,可以去搜索一下selenium接管浏览器,文中需要用到
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time, requests
import cv2
import numpy as np
import re
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
zh = webdriver.Chrome(r'E:\pycharm\pythonProject\Reptile\自动化测试\chromedriver.exe', chrome_options=chrome_options)
zh.get('https://www.zhihu.com/signin?next=%2F')
#登录
def login():
element = zh.find_element_by_css_selector('[class="SignFlow-tab"]').click()
zh.implicitly_wait(3)
username = zh.find_element_by_css_selector('[placeholder="手机号或邮箱"]')
zh.implicitly_wait(3)
username.send_keys('admin') #此处键入用户名
time.sleep(2)
password = zh.find_element_by_css_selector('[placeholder="密码"]')
zh.implicitly_wait(3)
password.send_keys('admin') #此处键入密码
time.sleep(2)
loginbutton = zh.find_element_by_css_selector('[type="submit"]').click()
#获取验证图片
def hqimage():
zh.switch_to.frame('tcaptcha_iframe')
time.sleep(2)
bj_img =zh.find_element_by_xpath('/html/body/div/div[3]/div[2]/div/div[2]/img').get_attribute('src') #获取背景图链接
xhd_img = zh.find_element_by_xpath('/html/body/div/div[3]/div[2]/div/div[3]/img').get_attribute('src') #获取小黑块链接
print(bj_img)
print(xhd_img)
with open('./bj.jpg', mode='wb') as f: #保存背景图图片到本地
f.write(requests.get(bj_img).content)
f.close()
with open('./xhd.jpg', mode='wb') as f: #保存小黑块图片到本地
f.write(requests.get(xhd_img).content)
f.close()
k=1
#处理验证图片
def climage():
bj = cv2.imread('./bj.jpg') #打开待处理图片
xhd =cv2.imread('./xhd.jpg')
bj = cv2.cvtColor(bj, cv2.COLOR_BGR2GRAY)# 灰度处理
xhd = cv2.cvtColor(xhd, cv2.COLOR_BGR2GRAY)
time.sleep(3)
# 去掉滑块黑色部分
xhd = xhd[xhd.any(1)] # 0表示黑色,1表示高亮部分
time.sleep(3)
# 匹配->cv图像匹配算法
result = cv2.matchTemplate(bj, xhd, cv2.TM_CCOEFF_NORMED) # match匹配,Template模板;精度高,速度慢的方法
index_max = np.argmax(result) # 返回的是一维的位置,最大值索引
time.sleep(3)
# 反着推最大值的二维位置,和opencv是相反的
x, y = np.unravel_index(index_max, result.shape)
print("二维中坐标的位置:", x, y)
print("正在进行第%s次滑动验证" % k)
drop = zh.find_element_by_xpath('/html/body/div/div[3]/div[2]/div[2]/div[2]/div')
ActionChains(zh).drag_and_drop_by_offset(drop, xoffset=x+26, yoffset=0).perform()
time.sleep(1)
if __name__ == '__main__':
login()
while 1:
hqimage()
climage()
zh.switch_to.default_content()
if '首页' in zh.title:
break
else:
print('第%s次验证失败...' % k, '\n')
k = k+1
time.sleep(5)
|