python 尝试在spider generator上用零碎、奇怪的行为响应Amazon的验证码

c86crjj0  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(79)

我创建了一个爬虫亚马逊为学习的原因,但它是由他们的验证码捕获.所以我做了验证码解决器,但我有麻烦,以响应验证码形式.问题是,如果我把一个yield FormRequest的方法,它似乎没有被调用.

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)

在亚马逊的验证码表格下面

<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>

我在代码里放了两个日志。
第一个在verify_if_captcha中:

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

这张是印刷的
solve_captcha内部的第二个:

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

这个从来没有印过
有人能帮帮我吗?

cgh8pdjw

cgh8pdjw1#

目前,表单请求对象从未返回给Scrapy进行处理。
self.solve_captcha(response, self.parse)替换为yield from self.solve_captcha(response, self.parse)

相关问题