solver Module

This module contains the solver itself, AmazonCaptcha instance.

Examples

Constructor usage.

from amazoncaptcha import AmazonCaptcha

captcha = AmazonCaptcha('captcha.jpg')
solution = captcha.solve()

# Or: solution = AmazonCaptcha('captcha.jpg').solve()

Using a selenium webdriver.

from amazoncaptcha import AmazonCaptcha
from selenium import webdriver

driver = webdriver.Chrome() # This is a simplified example
driver.get('https://www.amazon.com/errors/validateCaptcha')

captcha = AmazonCaptcha.fromdriver(driver)
solution = captcha.solve()

Keeping logs of unsolved captcha.

from amazoncaptcha import AmazonCaptcha

captcha = ...
solution = captcha.solve(keep_logs=True)

The AmazonCaptcha Class

class amazoncaptcha.solver.AmazonCaptcha(img, image_link=None, devmode=False)[source]
__init__(img, image_link=None, devmode=False)[source]

Initializes the AmazonCaptcha instance.

Parameters:
  • img (str or io.BytesIO) – Path to an input image OR an instance of BytesIO representing this image.
  • image_link (str, optional) – Used if AmazonCaptcha was created using fromdriver class method. Defaults to None.
  • devmode (bool, optional) – If set to True, instead of ‘Not solved’, unrecognised letters will be replaced with dashes.
_find_letters()[source]

Extracts letters from an image using found letter boxes.

Populates ‘self.letters’ with extracted letters being PIL.Image instances.

_monochrome()[source]

Makes a captcha pure monochrome.

Literally says: “for each pixel of an image turn codes 0, 1 to a 0, while everything in range from 2 to 255 should be replaced with 255”. *All the numbers stay for color codes.

_save_letters()[source]

Transforms separated letters into pseudo binary.

Populates ‘self.letters’ with pseudo binaries.

_translate()[source]

Finds patterns to extracted pseudo binary strings from data folder.

Literally says: “for each pseudo binary scan every stored letter pattern and find a match”.

Returns:
a solution if there is one OR
’Not solved’ if devmode set to False OR a solution where unrecognised letters will be replaces with dashes
Return type:str
classmethod fromdriver(driver, devmode=False)[source]

Takes a screenshot from your webdriver, crops the captcha, and stores it into bytes array, which is then used to create an AmazonCaptcha instance.

This also means avoiding any local savings.

Parameters:
  • driver (selenium.webdriver.*) – Webdriver with opened captcha page.
  • devmode (bool, optional) – If set to True, instead of ‘Not solved’, unrecognised letters will be replaced with dashes.
Returns:

Instance created based on webdriver.

Return type:

AmazonCaptcha

Requests the given link and stores the content of the response as io.BytesIO that is then used to create AmazonCaptcha instance.

This also means avoiding any local savings.

Parameters:
  • link (str) – Link to Amazon’s captcha image.
  • devmode (bool, optional) – If set to True, instead of ‘Not solved’, unrecognised letters will be replaced with dashes.
  • timeout (int, optional) – Requests timeout.
Returns:

Instance created based on the image link.

Return type:

AmazonCaptcha

Raises:

ContentTypeError – If response headers contain unsupported content type.

Image link property is being assigned only if the instance was created using fromdriver or fromlink class methods.

If you have created an AmazonCaptcha instance using the constructor, the property will be equal to None which triggers the warning.

solve(keep_logs=False, logs_path='not-solved-captcha.log')[source]

Runs the sequence of solving a captcha.

Parameters:
  • keep_logs (bool) – Not solved captchas will be logged if True. Defaults to False.
  • logs_path (str) – Path to the file where not solved captcha links will be stored. Defaults to “not-solved-captcha.log”.
Returns:

Result of the sequence.

Return type:

str