Search code examples
pythonhtmlseleniumselenium-webdriver2captcha

How can I programatically upload captcha images to this solving service in python (html given)?


I am writing a python program that needs to be able to solve captchas on a web site. I would like to use 2captcha. I have written a python script using selenium that will do everything I need to do except solve the captchas. When I click on the 2captcha.com "API" tab, this (along with other parameters) is what is shown:

You can upload your CAPTCHAs in two available formats:

Multipart and Base64.:

Multipad sample:

<form method="post" action="http://2captcha.com/in.php" enctype="multipart/form-data">
<input type="hidden" name="method" value="post">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file:
<input type="file" name="file">
<input type="submit" value="download and get the ID">
</form>

YOUR_APIKEY - is your key of 32 symbols length.

Base64 Sample:

<form method="post" action="http://2captcha.com/in.php">
<input type="hidden" name="method" value="base64">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file body in base64 format:
<textarea name="body">BASE64_FILE</textarea>
<input type="submit" value="download and get the ID">
</form>

YOUR_APIKEY - is your key of 32 symbols length.

BASE64_FILE - is the base 64 encoded image body.

I know python, and most of its scientific and mathematical modules well, but I am a bit new to web related programming. The code above looks like html. How would I make a python program carry out the html instructions above?


Solution

  • Look at the requests module

    url = 'http://2captcha.com/in.php'
    files = {'file': open('image.png', 'rb')}
    data = {'key': 'key', 'method': 'post'}
    r = requests.post(url, files=files, data=data)
    if r.ok:
        # do something with the response data