内附个人理解注释
示例代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
import os
import sys
path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(path)
from chaojiying import Chaojiying_Client
import requests
from hashlib import md5
from selenium.webdriver.common.by import By
class main():
def __init__(self):
self.url = '' # 爬取网址
self.distance = 0 # 缺块左边距离
self.key = 0
self.left = 0 # 填充块左边距离
self.left1 = 0 # 滑动按钮左边距离
self.cz = 0 #填充块和缺块的差值(左边)
# 启动浏览器
def Launch_browser(self):
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver, 10, 0.5)
self.driver.get(self.url)
self.driver.find_element(By.XPATH,'/html/body/div/div[4]/div/div[4]/div/div[2]').click()
Phone_Number = self.driver.find_element(By.XPATH,
'/html/body/div/div[4]/div/div[5]/form[2]/div[1]/div/div/input') #手机号输入框
Verification_Code = self.driver.find_element(By.XPATH,
'/html/body/div/div[4]/div/div[5]/form[2]/div[2]/div/input') #验证码输入框
Code_Button = self.driver.find_element(By.XPATH,
'/html/body/div/div[4]/div/div[5]/form[2]/div[2]/button') #获取验证码按钮
Phone_Number.send_keys('13766327523') #设置手机号
Code_Button.click()
# 等待className为geetest_slider_button的元素在元素表中出现
time.sleep(5)
element = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'slider-bg'))
)
self.slider = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'slider-btn')))
self.sliderImg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'slider-draw')))
self.left = int(self.sliderImg.value_of_css_property('left').split('px')[0])
self.left1 = int(self.slider.value_of_css_property('left').split('px')[0])
self.driver.execute_script("document.getElementsByClassName('slider-draw')[0].style.display='none'");
# 截图元素
element_screenshot = element.screenshot_as_png
self.driver.execute_script("document.getElementsByClassName('slider-draw')[0].style.display='block'");
with open('screenshot.png', 'wb') as file:
file.write(element_screenshot)
print(self.left)
# 超级鹰
def cjy(self):
# 用户中心>>软件ID 生成一个替换 910001
self.chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰密码', '910001')
# 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
im = open('screenshot.png', 'rb').read()
# 9101 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
# 咨询了一下滑动验证码是选择9101
re = self.chaojiying.PostPic(im, 9101)
print(re)
# 减去一半滑块长度
self.distance = int(re['pic_str'].split(',')[0]) - 25
self.cz = self.distance - int(self.sliderImg.value_of_css_property('left').split('px')[0])
print(self.distance,self.cz)
self.im_id = re['pic_id']
print(self.im_id)
# 模拟移动
def move(self):
# 点击和按住
xxxx = int(abs(self.left - self.left1)) # 获取滑块按钮和填充块左边距离的插值
target_x = int(self.cz + xxxx) # 插值加上滑块按钮和填充块左边距离的插值就是滑动的偏移量
print(target_x)
ActionChains(self.driver).click_and_hold(self.slider).perform()
ActionChains(self.driver).move_by_offset(xoffset=target_x, yoffset=0).perform()
time.sleep(2)
# 松开鼠标
ActionChains(self.driver).release().perform()
# 关闭浏览器
def quit(self):
time.sleep(5)
self.driver.quit()
# main方法
def main(self):
self.Launch_browser()
self.cjy()
self.move()
self.quit()
if __name__ == '__main__':
ma = main()
ma.main()
标签:python,self,driver,验证码,element,爬取,slider,import,div
From: https://blog.csdn.net/weixin_59685936/article/details/140875347