首页 > 编程语言 >python 滑动验证码爬取思路

python 滑动验证码爬取思路

时间:2024-08-03 09:59:28浏览次数:9  
标签:python self driver 验证码 element 爬取 slider import div

参考文章:python爬虫之滑动验证码[完整版] - 简书

内附个人理解注释

示例代码:

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

相关文章

  • 使用Python自动将照片文件夹转换为PowerPoint幻灯片
    在这个数字时代,我们经常需要快速创建照片幻灯片来展示我们的回忆或工作成果。今天,我们将探讨如何使用Python来自动化这个过程,将一个文件夹中的所有照片转换为一个精美的PowerPoint演示文稿,每张照片占据一页,并以文件名作为标题。C:\pythoncode\new\jpeginsertppt.py全......
  • Python的GDAL库绘制多波段、长时序遥感影像时间曲线图
      本文介绍基于Python中的gdal模块,对大量长时间序列的栅格遥感影像文件,绘制其每一个波段中、若干随机指定的像元的时间序列曲线图的方法。  在之前的文章中,我们就已经介绍过基于gdal模块,对大量多时相栅格图像,批量绘制像元时间序列折线图的方法。不过当时文章中的需求,每1个时......
  • 仅从 Dash Python 中的表中提取过滤后的数据
    我用DashPython构建了一个网站,并将csv文件的所有数据显示在一个可以过滤的表中。我希望用户能够从表中提取数据。当没有过滤器时,我希望他能够提取完整的数据,当他过滤数据时,我希望他能够提取过滤后的数据。为此,我使用dcc.download组件,它是工作得很好,我还使用d......
  • 如何使用pipenv解决Python包依赖关系?
    我正在使用pipenv处理Python包依赖项。Python包使用两个包(名为pckg1和pckg2),它们依赖于名为pckg3的同一个包,但来自两个不同的包版本显示依赖关系树:$pipenvgraphpckg1==3.0.0-pckg3[required:>=4.1.0]......
  • 存在的开放路径不起作用 - python
    代码获取测试logs\log.jsonl的根路径,该路径在图像中显然在那里,但无法打开它#doesntworkdir_path=os.path.dirname(os.path.realpath(__file__))withopen(os.path.join(dir_path,"logs\\log.jsnol"),'r')asjsondata:data=json.load(jsondata)wit......
  • 如何在系统PATH中永久添加路径? - Python
    我正在为我的一个项目创建一个安装程序,下载该项目的二进制文件(在ZIP文件中),然后将其解压缩到操作系统程序文件夹中的一个目录中,我想将此目录添加到|||。我的脚本是一个带有UI的安装程序,其目的是编译为PATH/可执行文件。如果你能找到我一种.exe平台无关......
  • python数据分析与可视化基础
    一、数据分析介绍:1.数据分析含义:数据分析是指用适当的统计分析方法对收集来的大量数据进行分析,将它们加以汇总和理解并消化,以求最大化地开发数据的功能,发挥数据的作用。数据分析是为了提取有用信息和形成结论而对数据加以详细研究和概括总结的过程。数据分析的数学基础在20世纪早......
  • 为什么要设置 os.environ[“PYTHONHASHSEED“] = “0“,这样做具体会影响哪些随机值?
    ......
  • Python,Geopandas报错,AttributeError: The geopandas.dataset has been deprecated and
    Python版本3.9,Geopandas版本1.0.1问题描述:这是执行的代码,importpandasaspdimportgeopandasimportmatplotlib.pyplotaspltworld=geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))world.plot()plt.show()这是报错信息,Traceback(mo......
  • Python,Maskrcnn训练,cannot import name ‘saving‘ from ‘keras.engine‘ ,等问题集合
    Python版本=3.9,tensorflow=2.11.0,keras==2.11.0问题一、module'keras.engine'hasnoattribute'Layer'Traceback(mostrecentcalllast):File"C:\Users\Administrator\Desktop\20240801\代码\test.py",line16,in<module>......