我创建了一个爬虫亚马逊为学习的原因,但它是由他们的验证码捕获.所以我做了验证码解决器,但我有麻烦,以响应验证码形式.问题是,如果我把一个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="/Capodarte-Chinelo-Preto-38/dp/B07N13Q5F2/?th=1&psc=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!')
这个从来没有印过
有人能帮帮我吗?
1条答案
按热度按时间cgh8pdjw1#
目前,表单请求对象从未返回给Scrapy进行处理。
将
self.solve_captcha(response, self.parse)
替换为yield from self.solve_captcha(response, self.parse)
。