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()
Using a captcha image link directly.¶
from amazoncaptcha import AmazonCaptcha
link = 'https://images-na.ssl-images-amazon.com/captcha/usvmgloq/Captcha_kwrrnqwkph.jpg'
captcha = AmazonCaptcha.fromlink(link)
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:
-
classmethod
fromlink
(image_link, devmode=False, timeout=120)[source]¶ 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: Returns: Instance created based on the image link.
Return type: Raises: ContentTypeError
– If response headers contain unsupported content type.
-
image_link
¶ 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.
-