Trying to response Amazon's Captcha with scrapy, strange behavior on spider generator

Question:

I’m creating a crawler to Amazon for study reason, but it is being caught by their captcha.
So I have made a captcha solver, but I’m having trouble to response the captcha form.
The problem is that if I put a yeild FormRequest in the method, it seems to not be called.

class Havaianas2Spider(scrapy.Spider):
    name = 'coleta_dados_grafo'
    rank_path = sorted([x for x in os.listdir('scraps') if 'links_base' in x], reverse=True)[0]
    lista_links = pd.read_csv('scraps/' + rank_path)
    start_urls = lista_links['links'].values
    custom_settings = {'FEED_URI': "scraps/produtos_%(time)s.csv",
                       'FEED_FORMAT': 'csv'}
    final_path = '/?th=1&psc=1'

    def base_path_get(self, response):
        dp_idx = response.request.url.find('/dp/') + 4
        base_path = response.request.url[:dp_idx]
        return base_path

    def solve_captcha(self, response, origin_method):
        self.logger.info('SOLVING CAPTCHA!')
        captcha_url = response.xpath('//div[@class="a-row a-text-center"]/img/@src').extract_first()
        img = load_url(captcha_url)
        captcha_string = break_captcha(img)
        img.save('C:/Users/Bruno Aquino/Documents/ecom_scraper/amazon_scraper/amazon_scraper/captchas/{}.jpg'.format(
            captcha_string))
        yield FormRequest.from_response(response,
                                        formdata={'field-keywords': captcha_string},
                                        callback=origin_method)

    def verify_if_captcha(self, response):
        captcha_url = response.xpath('//div[@class="a-row a-text-center"]/img/@src').extract_first()

        if captcha_url:
            self.logger.info('PAGE {} GOT BY CAPTCHA!'.format(response.request.url))
            return True
        else:
            return False

    def parse(self, response):

        captcha = self.verify_if_captcha(response)

        if captcha:
            self.solve_captcha(response, self.parse)

        else:
            base_path = self.base_path_get(response)

            asin_colors = response.xpath('//div[@id="cerberus-data-metrics"]/@data-asin').extract() +
            [x[4:14] for x in response.xpath('//li[contains(@id,"color_name_")]/@data-dp-url').extract() if '/dp/' in x]

            for asin in asin_colors:
                new_path = base_path + asin + self.final_path

                if asin:
                    yield scrapy.Request(
                        response.urljoin(new_path),
                        callback=self.parse_l2)

below the Amazon’s captcha form

<form method="get" action="/errors/validateCaptcha" name="">
                            <input type=hidden name="amzn" value="Xnnhl7YtGcH60X2yPaN7eA==" /><input type=hidden name="amzn-r" value="&#047;Capodarte&#045;Chinelo&#045;Preto&#045;38&#047;dp&#047;B07N13Q5F2&#047;?th&#061;1&amp;psc&#061;1" />
                            <div class="a-row a-spacing-large">
                                <div class="a-box">
                                    <div class="a-box-inner">
                                        <h4>Type the characters you see in this image:</h4>
                                        <div class="a-row a-text-center">
                                            <img src="https://images-na.ssl-images-amazon.com/captcha/yniigayf/Captcha_kbknwlcmvm.jpg">
                                        </div>
                                        <div class="a-row a-spacing-base">
                                            <div class="a-row">
                                                <div class="a-column a-span6">
                                                    <label for="captchacharacters">Type characters</label>
                                                </div>
                                                <div class="a-column a-span6 a-span-last a-text-right">
                                                    <a onclick="window.location.reload()">Try different image</a>
                                                </div>
                                            </div>
                                            <input autocomplete="off" spellcheck="false" id="captchacharacters" name="field-keywords" class="a-span12" autocapitalize="off" autocorrect="off" type="text">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="a-section a-spacing-extra-large">

                                <div class="a-row">
                                    <span class="a-button a-button-primary a-span12">
                                        <span class="a-button-inner">
                                            <button type="submit" class="a-button-text">Continue shopping</button>
                                        </span>
                                    </span>
                                </div>

                            </div>
                        </form>

I put two logs into the code.

The first on inside the verify_if_captcha:

self.logger.info('PAGE {} GOT BY CAPTCHA!'.format(response.request.url))

This one is printed

The second inside the solve_captcha:

self.logger.info('SOLVING CAPTCHA!')

This one is never printed

Can some one help me, please?

Asked By: Bruno Aquino

||

Answers:

Currently, your form request object is never returned to Scrapy for handling.

Replace self.solve_captcha(response, self.parse) by yield from self.solve_captcha(response, self.parse).

Answered By: Gallaecio