【问题标题】:Handling Error Pages in Scrapy在 Scrapy 中处理错误页面
【发布时间】:2014-01-02 06:36:22
【问题描述】:

我在 start_urls

中有一个网址

爬虫第一次加载页面时,首先显示403错误页面,之后爬虫关闭。

我需要做的是在该页面上填写一个验证码,然后它会让我访问该页面。我知道如何编写绕过验证码的代码,但是我应该将这段代码放在我的蜘蛛类中的什么位置?

遇到相同问题时,我也需要在其他页面上添加它。

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

from scrapy.selector import Selector

class MySpider(CrawlSpider):
    name = "myspider"
    allowed_domains = ["mydomain.com"]
    start_urls = ["http://mydomain.com/categories"]
    handle_httpstatus_list = [403] #Where do I now add the captcha bypass code?
    download_delay = 5
    rules = [Rule(SgmlLinkExtractor(allow=()), callback='parse_item')]

    def parse_item (self, response):
        pass

【问题讨论】:

    标签: python web-crawler scrapy


    【解决方案1】:

    设置handle_httpstatus_list403 视为成功响应码:

    class MySpider(CrawlSpider):
        handle_httpstatus_list = [403]
    

    至于绕过实际验证码,您需要覆盖parse 以不同方式处理具有403 响应代码的所有页面:

    def parse(self, response):
        if response.status_code == 403:
            return self.handle_captcha(response):
    
        yield CrawlSpider.parse(self, response)
    
    def handle_captcha(self, response):
        # Fill in the captcha and send a new request
        return Request(...)
    

    【讨论】:

    • 抱歉,这不起作用。 Scrapy 在遇到 403 错误时仍在退出。
    • @Crypto:爬虫会因为各种原因关闭。完整的追溯是什么?
    • 没有回溯。当它在起始页面上遇到 403 错误时,它只是退出说“INFO:Closing spider (finished)”
    • 我最近重命名了蜘蛛文件。在我删除了相应的 .pyc 文件后,更改就会反映出来。所以现在它将 403 视为有效页面,我在哪里添加我的处理程序来编写验证码?
    • @Crypto:如果 403 响应代码从未发送过,你会去哪里。就像我说的,这取决于你的源代码。
    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多