擅长:python、mysql、java
<p>我使用的方法通常很有效,如下所示(只是一个要点,您需要添加您的具体细节):</p>
<p>步骤1-获取验证码url(并保留表单的响应以便以后使用)</p>
<pre><code>def parse_page_with_captcha(response):
captcha_url = response.xpath(...)
data_for_later = {'captcha_form': response} # store the response for later use
return Request(captcha_url, callback=self.parse_captcha_download, meta=data_for_later)
</code></pre>
<p>第2步-现在scrapy将下载图片,我们必须在scrapy回调中正确处理它</p>
^{pr2}$
<p><strong>重要细节</strong></p>
<p>受验证码保护的表单需要某种方式将验证码图像与看到并回答验证码的特定用户/客户机链接起来。这通常是使用基于cookie的会话或隐藏在captcha表单中的特殊参数/图像标记来完成的。在</p>
<p>刮板代码必须小心不要破坏这个链接,否则它将回答一些验证码,但不是它必须的验证码。在</p>
<p><strong>为什么Verz1Lka发布的两个示例不起作用?</strong></p>
<p>在urllib.url检索这种方法完全不受刮痧的影响。虽然这通常是个坏主意(这不会利用scrapys调度等的好处),但这里的主要问题是:这个请求将完全不在目标站点用来跟踪哪个captcha被发送到特定浏览器的会话cookie、url参数等之外工作。在</p>
<p>另一方面,使用图像管道的方法在Scrapy的规则中运行得很好,但是这些图像下载被安排在稍后的时间进行,因此在需要时无法下载captcha。在</p>