我正在尝试captcha
从网站的表单中打破,但是此验证码是动态的,没有URL,而是类似这样的内容
src="captcha?accion=image"
这里最好的选择是什么?我读过类似使用中间件之类的东西。我也知道可以用Selenium或Splash或其他浏览器驱动程序(屏幕截图)来完成,但是我想只用来完成Scrapy
,当然这是可能的。
这是使用anticaptcha和PIL绕过指定的完整解决方案。captcha
由于这种动态性captcha
,我们需要抓取img
包含元素的打印屏幕captcha
。为此,我们使用save_screenshot()
和PIL
裁剪并保存<img name="imagen"...
到磁盘(captcha.png
)。
然后,我们提出captcha.png
来anti-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] 删除。
我来说两句