【问题标题】:How to make Scrapy XmlFeed Spider Faster如何更快地制作 Scrapy XmlFeed Spider
【发布时间】:2019-01-30 00:39:12
【问题描述】:

我正在抓取的 xml 提要有大约一千个项目。我想知道是否有一种方法可以拆分负载或另一种方法来显着减少运行时间。目前迭代下面链接中的所有 xml 需要两分钟。非常感谢任何建议或建议。

示例:https://www.cityblueshop.com/sitemap_products_1.xml

from scrapy.spiders import XMLFeedSpider
from learning.items import TestItem
class MySpider(XMLFeedSpider):
    name = 'testing'
    allowed_domains = ['www.cityblueshop.com']
    start_urls = ['https://www.cityblueshop.com/sitemap_products_1.xml'] 

    namespaces = [('n', 'http://www.sitemaps.org/schemas/sitemap/0.9')]
    itertag = 'n:url'
    iterator = 'xml'


    def parse_node(self, response, node):

        item = TestItem()
        item['url'] = node.xpath('.//n:loc/text()').extract()


        return item

所有项目的运行时间为两分钟。有什么方法可以让使用 Scrapy 更快?

【问题讨论】:

    标签: python xml web-scraping scrapy


    【解决方案1】:

    我在本地测试了以下蜘蛛:

    from scrapy.spiders import XMLFeedSpider
    
    class MySpider(XMLFeedSpider):
        name = 'testing'
        allowed_domains = ['www.cityblueshop.com']
        start_urls = ['https://www.cityblueshop.com/sitemap_products_1.xml']
    
        namespaces = [('n', 'http://www.sitemaps.org/schemas/sitemap/0.9')]
        itertag = 'n:url'
        iterator = 'xml'
    
    
        def parse_node(self, response, node):
            yield {'url': node.xpath('.//n:loc/text()').get()}
    

    运行时间不到 3 秒,包括 Scrapy 核心启动等一切。

    请确保时间没有花在其他地方,例如在您从中导入项目子类的 learning 模块中。

    【讨论】:

      【解决方案2】:

      尝试增加CONCURRENT_REQUESTS、CONCURRENT_REQUESTS_PER_DOMAIN、CONCURRENT_REQUESTS_PER_IP,例如:https://doc.scrapy.org/en/latest/topics/settings.html#concurrent-requests-per-domain 但请记住,除了高速之外,它还会导致较低的成功率,例如许多 429 响应、禁令等。

      【讨论】:

      • 所有内容都在同一个页面上并且只是通过它进行解析,这只是主 xml 页面所需的一个请求
      • 糟糕,我的错,误读了。在这种情况下是的,这些设置没用,对不起。
      猜你喜欢
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多