Menu

Python을 사용하여 이미지 CAPTCHA가 포함된 양식을 제출하는 방법


학습 내용

  • 초보자를 위해 마련된 이 튜토리얼에서는 이미지 CAPTCHA를 사용하여 양식을 제출하는 기본 원칙을 살펴보도록 하겠습니다.
  • 이미지 CAPTCHA가 포함된 데모 양식을 제출합니다.
  • 브라우저로 양식을 분석하여 처음부터 시작합니다. Anti CAPTCHA 서비스를 통해 양식 데이터를 추출하고 CAPTCHA를 푸는 코드를 작성합니다.
  • 양식 데이터를 제출하고 긍정적인 결과를 확인하는 코드로 마무리합니다.

소스 코드:

from anticaptchaofficial.imagecaptcha import *
import requests
import base64


def find_between(s, start, end):
    return (s.split(start))[1].split(end)[0]


response = requests.get("https://anti-captcha.com/demo?page=image_captcha")
base64str = find_between(response.text, ";base64,", "\">")
c_str = find_between(response.text, " name=\"c\" value=\"", "\">")

print(f"base64: {base64str}")
print(f"c_str: {c_str}")

# save base64 image to a temporary file
with open("tmp.file", "wb") as file:
    file.write(base64.urlsafe_b64decode(base64str))


solver = imagecaptcha()
solver.set_verbose(1)
solver.set_key("API_KEY_HERE")
captcha_text = solver.solve_and_return_solution("tmp.file")
if captcha_text != 0:
    print("captcha text "+captcha_text)
    res = requests.post("https://anti-captcha.com/demo/submit_image.php", data={
        "login": "testlogin",
        "pass": "testpass",
        "c": c_str,
        "captcha": captcha_text
    })
    if "test passed" in res.text:
        print("Our test passed!")
    if "Captcha test not passed" in res.text:
        print("Test NOT passed!")
else:
    print("task finished with error "+solver.error_code)

Github

https://github.com/anti-captcha/solving-captcha-concepts/blob/master/tutorial5.py