方案设计:
1.首先找个接码平台,使用其自动生成的手机号码来读取验证码
此处略
2.使用某工具识别滑块验证码
目前考虑用带带弟弟,能成功识别出滑块坐标。
import ddddocr
# 发送请求并下载图片
class DdddocrMethod:
def ddddocrPic(self):
det = ddddocr.DdddOcr()
with open('huakuai.jpg', 'rb') as f:
target_bytes = f.read()
with open('beijing.jpg', 'rb') as f:
background_bytes = f.read()
res = det.slide_match(target_bytes, background_bytes)
print(res)
return res['target'][0]/2
3.通过Selenium工具操作鼠标进行点击按钮等工作
实例代码:
import pyautogui
from selenium.webdriver import ActionChains
import random
# 发送请求并下载图片
class MovePic:
def movePic(self,driver, huakuai_element,move_length):
action_chains = ActionChains(driver)
# action_chains.drag_and_drop_by_offset(huakuai_element, res['target'][0], 0).perform()
action_chains.move_to_element(huakuai_element).click().perform()
# 获取元素在屏幕上的位置和大小
location = huakuai_element.location
# size = huakuai_element.size
# 计算元素中心的坐标位置
# element_x = location['x'] + size['width'] // 2
# element_y = location['y'] + size['height']
# print(location['x'])
# print(location['y'])
random_integer_init = random.randint(-10, 10)
# 使用 pyautogui 将鼠标移动到元素中心
pyautogui.moveTo(location['x'] +40 +random_integer_init, location['y']+390 +random_integer_init ,duration=0.1)
pyautogui.mouseDown()
random_integer_end = random.randint(-3, 3)
pyautogui.dragRel(move_length +random_integer_end,0+random_integer_end,button='left',duration=1)
pyautogui.dragRel(-random_integer_end,0,button='left',duration=0.1)
pyautogui.dragRel(-1,0,button='left',duration=0.1)
pyautogui.dragRel(2,0,button='left',duration=0.1)
# pyautogui.displayMousePosition()
注意一定要多移动几次,这样不容易被检测出来
4.操作接码平台API接口,获取验证码
5.数据填充,并点击提交按钮
import time
from selenium.webdriver.common.by import By
class SearchElement:
def search(self,driver,phoneNumber):
input_element = driver.find_element(By.XPATH, "/html/body/div/div/div/div[1]/div[2]/div[1]/input")
input_element.clear()
input_element.send_keys(phoneNumber)
button_element = driver.find_element(By.XPATH, "/html/body/div/div/div/div[1]/div[2]/div[2]/div/div[1]")
button_element.click()
time.sleep(1)
# 定位目标图片元素
huakuai_element = driver.find_element("xpath", "/html/body/div/div/div/div[4]/div/div[2]/div/div/div[1]/div[2]/img")
# 获取图片的URL
huakuai_image_url = huakuai_element.get_attribute("src")
# 定位目标图片元素
beijing_element = driver.find_element("xpath", "/html/body/div/div/div/div[4]/div/div[2]/div/div/div[1]/div[1]/img")
# 获取图片的URL
beijing_image_url = beijing_element.get_attribute("src")
return huakuai_image_url,beijing_image_url,huakuai_element
6.通过xpath识别返回数据的内容,保存结果到本地库中
检测弹出框的内容:
try:
wait = WebDriverWait(driver, 5)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), '发送成功')]")))
# 获取元素的文本内容
message = element.text
print("弹出消息内容:", message)
except TimeoutException:
print("等待超时,没有找到符合条件的元素")
continue
if message == '发送成功':
print("检测到发送成功了!手机号:" + phoneNumber)
getMeassage = GetMessage()
str = getMeassage.getMsg(token,phoneNumber)
if str.strip():
driver.quit()
continue
#验证:
label_element = driver.find_element(By.XPATH, "/html/body/div/div/div/div[4]/div/div[2]")
if label_element.get_attribute('innerText') == '您已注册,是否前往登录':
print("弹出消息内容:", label_element.get_attribute('innerText'))
cancel_element = driver.find_element(By.XPATH, "/html/body/div/div/div/div[4]/div/div[3]/button[2]")
try:
action_chains = ActionChains(driver)
action_chains.move_to_element(cancel_element).click().perform()
except:
print("移动鼠标失败")
#拉黑号码
setBlack = BlackListPhoneNumber()
setBlack.setPhone(token,phoneNumber)
continue
# 执行JavaScript代码来检查是否存在弹出消息javascript:void(0)
项目done