[*]OCR mis-reads (e.g. confusing “×” vs “x”)
[*]Timeouts after solving multiple CAPTCHAs in quick succession
[*]Session-expiration errors Это требует, чтобы я сбросил весь сеанс < /code> < /li>
< /ul>
То, что мой код Captcha-Solver выглядит как < /h3>
Ниже приведена основная функция, которую я использую, чтобы получить изображение Captcha, OT с easyoc, затем оценить простое математическое выражение. Это часто ошибочно экспрессирует символы или возвращает необработанные строки: < /p>
Код: Выделить всё
import re
import requests
import easyocr
ROOT = "https://judgments.ecourts.gov.in"
CAPTCHA_URL = ROOT + "/pdfsearch/vendor/securimage/securimage_show.php"
# Initialize the OCR reader only once
reader = easyocr.Reader(["en"], gpu=False)
def solve_captcha(session: requests.Session) -> str:
"""
1. Download the CAPTCHA image.
2. Pass raw bytes into easyocr for text extraction.
3. Parse the returned string as a simple arithmetic expression.
4. Return the computed result as a string.
"""
# Step 1: fetch image
res = session.get(CAPTCHA_URL, verify=False, timeout=30)
img_bytes = res.content
# Step 2: OCR the contents
ocr = reader.readtext(img_bytes)
if not ocr:
raise RuntimeError("OCR returned no text")
expr = ocr[0][1].strip() # e.g. "12 × 3"
# Step 3: parse "number operator number"
m = re.match(r"(\d+)\s*([+\-×Xx÷/])\s*(\d+)", expr)
if not m:
raise RuntimeError(f"Unexpected format from OCR: {expr}")
a_str, op, b_str = m.groups()
a, b = int(a_str), int(b_str)
# Step 4: evaluate
if op == "+": return str(a + b)
if op == "-": return str(a - b)
if op in ("×","X","x","*"): return str(a * b)
if op in ("÷","/"): return str(a // b)
raise RuntimeError(f"Unknown operator: {op}")
< /code>
Как я интегрирую его в свой скребок < /h3>
сразу после решения Captcha, я размещаю ответ на их конечную токну: < /p>
Подробнее здесь: [url]https://stackoverflow.com/questions/79688640/seeking-a-robust-python-scraper-or-library-to-bypass-captcha-on-ecourts-gov-in[/url]