Python Forum
Struggling To Work With Anti-Captcha API
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struggling To Work With Anti-Captcha API
#1
Hey guys, glad to be a member, I've been learning Python for the past few weeks and I'm a bit out of my depth.

I'm stuck trying to connect to the 2captcha anti-captcha API service.

Here's a look at the type of captcha I want to solve:

https://s1.postimg.org/17rs9tjhcv/Screenshot_3.jpg

I have a pre-made script that I've been trying to modify.. it's meant for recaptcha2 but I'm just trying to solve a normal captcha. I'm wondering if I should just attempt to re-write it from scratch.. here's the code:

import requests
from time import sleep
# Add these values
API_KEY = 'mykeyexample423cf8fc00adfcca'  # Your 2captcha API KEY
site_key = ''  # site-key, read the 2captcha docs on how to get this
url = ''  # example url
s = requests.Session()
# here we post site key to 2captcha to get captcha ID (and we parse it here too)
captcha_id = s.post("http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY, site_key, url)).text.split('|')[1]
# then we parse gresponse from 2captcha response
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id)).text
print("solving ref captcha...")
while 'CAPCHA_NOT_READY' in recaptcha_answer:
    sleep(5)
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id)).text
recaptcha_answer = recaptcha_answer.split('|')[1]
# we make the payload for the post data here, use something like mitmproxy or fiddler to see what is needed
payload = {
    'key': 'value',
    'gresponse': recaptcha_answer  # This is the response from 2captcha, which is needed for the post request to go through.
    }
# then send the post request to the url
response = s.post(url, payload)
# And that's all there is to it other than scraping data from the website, which is dynamic for every website.
The API is calling for the "BASE64_FILE is base64-encoded image body"

What the hell does that mean lol. Do they want a JPG version of the captcha?

I've isolated the URL where the captcha loads, I assumed I would be sending that over instead of an image but now I'm confused.

The site_key seems to be the string in the captcha screenshot after "?k=", it's a dynamically changing ID that I need to scrape as the captchas change correct?

"url" variable is just the page the captcha is located on?

Any help is much appreciated, I've been researching and trying to learn for about 8 hours today and still can't get it working LOL
Reply
#2
It means that you have to send them the image.  An image contains binary data which can't go in a url, so encoding it with base64 makes it safe to transfer.  Here's an example of how you can base64 something (base64 is included in the python standard library, no extra packages needed):
>>> import base64
>>> image = b"i'm the raw image data"
>>> base64.encodebytes(image)
b'aSdtIHRoZSByYXcgaW1hZ2UgZGF0YQ==\n'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  signal anti-aliasing frohr 0 1,138 May-23-2022, 05:18 PM
Last Post: frohr
  Struggling with Juggling JSON Data SamWatt 7 1,886 May-09-2022, 02:49 AM
Last Post: snippsat
  Syntax errors: Struggling to setup enviroment and load packages AH56 5 2,780 Jun-30-2021, 01:01 PM
Last Post: AH56
  Slider puzzle captcha resolver, how do this? dw0rd1337 0 3,475 Jan-04-2021, 11:55 PM
Last Post: dw0rd1337
  Struggling for the past hour to define function and call it back godlyredwall 2 2,217 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  struggling with != statements CallumRoberts2004 2 1,538 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  I’m Flat out struggling to understand list indexes gr3yali3n 7 2,905 Jul-20-2020, 07:18 PM
Last Post: princetonits
  Struggling with nested list gr3yali3n 3 2,303 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Question about a Python(?) captcha AudiRS4 3 2,241 Apr-10-2020, 05:33 PM
Last Post: micseydel
  Struggling to exit this while loop fatherted99 5 2,474 Feb-08-2020, 07:46 PM
Last Post: fatherted99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020