首页 > 解决方案 > Scrapy SitemapSpider在干净关闭后不继续

问题描述

我正在使用 JOBSDIR 使用此处的文档运行一个scrapy站点地图蜘蛛:https ://docs.scrapy.org/en/latest/topics/jobs.html 。

但是,我会彻底关机,按 ctrl + c(我已经仔细检查了多次,这是彻底关机,我没有发送 ctrl+c 两次),蜘蛛将无法继续。输出将显示“过滤的重复请求:example.com/sitemap.xml”,但在 JOBSDIR 文件夹中仍然会有大量看不见的请求和一个非常大的 requests.queue 文件。

为什么scrapy会过滤掉起点站点地图而不是使用requests.queue?显然,如果它过滤掉我提供的唯一网址,即站点地图,它将永远无法到达任何地方。有任何想法吗?

编辑:我正在做的一件事是从 python 脚本启动我的蜘蛛,而不是从 scrapy 命令行启动。这样做,也许我没有通过scrapy推荐的-s参数。“-s”有什么作用?这个变量的文档在哪里?

    process = CrawlerProcess({'JOBDIR': '.jobs/alexa_site_' + str(alexa_site_id)})

    MySpider.set_settings(alexa_site)
    MySpider.custom_settings[
        'USER_AGENT'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"

    process.crawl(MySpider, alexa_site_id=alexa_site_id)
    process.start()

这是蜘蛛代码:

class MySpider(SitemapSpider):
    custom_settings = {
        'RANDOMIZE_DOWNLOAD_DELAY': True,
        'MEMUSAGE_ENABLED': True,
        'DOWNLOAD_TIMEOUT': 20,
        'DEPTH_LIMIT': 100000,
        'LOG_LEVEL': 'CRITICAL',
        'DOWNLOADER_MIDDLEWARES': {
            'rotating_proxies.middlewares.RotatingProxyMiddleware': 610,
            'rotating_proxies.middlewares.BanDetectionMiddleware': 620,
        },
        'ROTATING_PROXY_BAN_POLICY': 'spiders.classes.proxies.policy.MyPolicy',
        'ROTATING_PROXY_PAGE_RETRY_TIMES': 20,
        'ROTATING_PROXY_LOGSTATS_INTERVAL': 5,
        'ROTATING_PROXY_CLOSE_SPIDER': True,
        'RETRY_HTTP_CODES': [500, 502, 503, 504, 522, 524, 408, 403],
        'DEPTH_PRIORITY': 1,
        'SCHEDULER_DISK_QUEUE': 'scrapy.squeues.PickleFifoDiskQueue',
        'SCHEDULER_MEMORY_QUEUE': 'scrapy.squeues.FifoMemoryQueue',
        'TELNETCONSOLE_USERNAME': 'scrapy',
        'TELNETCONSOLE_PASSWORD': 'scrapy',
        'DUPEFILTER_DEBUG': True
    }

    name = None
    allowed_domains = []
    sitemap_urls = ['https://www.example.com/sitemap.xml']

    def parse(self, response):
        le = LinkExtractor()
        links = le.extract_links(response)
        for link in links:
            yield response.follow(link.ur, self.parse)

标签: pythonweb-scrapingscrapy

解决方案


推荐阅读