ad-m / python-anticaptcha

Client library for solve captchas with Anticaptcha.com support.
http://python-anticaptcha.readthedocs.io/en/latest/
MIT License
223 stars 51 forks source link

How to solve captcha on website with ImageToTextTask #83

Closed MacMarde closed 4 years ago

MacMarde commented 4 years ago

I am using selenium with python and want to solve a text captcha The captcha can NOT be read from the URL with something like:

url = browser.find_element_by_id('captchaImg').get_attribute("src")
task = ImageToTextTask(session.get(url, stream=True).raw)

It is not working because I get a different captcha from the one I see on the website.

So I do need to find a way to read the captcha image from the website directly.

I can get the captcha image and read the base64 image data with:

ele_captcha = browser.find_element_by_id('captchaImg') 
img_captcha_base64 = ele_captcha.screenshot_as_base64

Is there a way to use this base64 data with python-anticaptcha ImageToTextTask?

def solve_captcha(img_captcha_base64):    
    client = AnticaptchaClient(api_key)
    task = ImageToTextTask(img_captcha_base64)
    job = client.createTask(task)
    job.join()
    return job.get_captcha_text()

I am still trying. I would appreciate any help.

MacMarde commented 4 years ago

I have found a solution. Thanks to help in the chat on gitter @sameroom It is something like:

from io import BytesIO
import base64

ele_captcha = browser.find_element_by_id('captchaImg') 
fp = BytesIO(base64.b64decode(ele_captcha.screenshot_as_base64))

client = AnticaptchaClient(api_key)
task = ImageToTextTask(fp)
job = client.createTask(task)
job.join()
return job.get_captcha_text()