动态验证码

天使LB

我正在尝试captcha从网站的表单中打破,但是此验证码是动态的,没有URL,而是类似这样的内容

src="captcha?accion=image"

这里最好的选择是什么?我读过类似使用中间件之类的东西。我也知道可以用Selenium或Splash或其他浏览器驱动程序(屏幕截图)来完成,但是我想只用来完成Scrapy,当然这是可能的。

佩德罗·洛比托

这是使用anticaptchaPIL绕过指定的完整解决方案captcha

由于这种动态性captcha,我们需要抓取img包含元素的打印屏幕captcha为此,我们使用save_screenshot()PIL裁剪并保存<img name="imagen"...到磁盘(captcha.png)。
然后,我们提出captcha.pnganti-captcha,将返回的解决方案,即:

from PIL import Image
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
from selenium import webdriver

def get_captcha():
    captcha_fn = "captcha.png"
    element = driver.find_element_by_name("imagen") # element name containing the catcha image
    location = element.location
    size = element.size
    driver.save_screenshot("temp.png")

    x = location['x']
    y = location['y']
    w = size['width']
    h = size['height']
    width = x + w
    height = y + h

    im = Image.open('temp.png')
    im = im.crop((int(x), int(y), int(width), int(height)))
    im.save(captcha_fn)

    # request anti-captcha service to decode the captcha

    api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' # api key -> https://anti-captcha.com/
    captcha_fp = open(captcha_fn, 'rb')
    client = AnticaptchaClient(api_key)
    task = ImageToTextTask(captcha_fp)
    job = client.createTask(task)
    job.join()
    return job.get_captcha_text()

start_url = "YOU KNOW THE URL"
driver = webdriver.Chrome()
driver.get(start_url)
captcha = get_captcha()
print( captcha )

输出:

ifds

captcha.png

在此处输入图片说明


笔记:

  • 自行负责使用(聪明)
  • 您可以通过适当地处理异常来改进代码。
  • anticaptcha是一项付费服务(0.5 $ / 1000 imgs);
  • 我不隶属于anticaptcha

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章