【问题标题】:Scrapy doesn't scrape items from my URLs : Crawled (200) / Referer : NoneScrapy 不会从我的 URL 中抓取项目:已抓取 (200) / 引用者:无
【发布时间】:2018-08-05 23:09:53
【问题描述】:

我正在尝试从一个网站抓取几个页面。为此,我有不同的起始 URL 和爬取下一页的方法。 问题是蜘蛛不会scrape 项目并且似乎没有爬取指定的页面。我没有结果。 你有什么办法解决这个问题吗?

这是代码

    class ListeCourse_level1(scrapy.Spider):
        name = nom_robot
        allowed_domains = domaine
        
        start_urls = url_lister()
        print(start_urls)
        print('-----------------------------')
    
        def parse(self, response):    
            
            selector = Selector(response)    
            
            for unElement in response.xpath('//*[@id="td-outer-wrap"]/div[3]/div/div/div[1]/div/div[2]/div[3]/table/tbody/tr'): 
                loader = ItemLoader(JustrunlahItem(), selector=unElement)
                
                loader.add_xpath('eve_nom_evenement', './/td[2]/div/div[1]/div/a/text()')
                loader.add_xpath('eve_date_deb', './/td[1]/div/text()')
loader.default_input_processor = MapCompose(string) 
                loader.default_output_processor = Join()
            
                yield loader.load_item()

shell 窗口的提取

--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
2018-02-26 14:13:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.justrunlah.com/running-events-calendar-malaysia/page/9/> (referer: None)
--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
2018-02-26 14:13:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.justrunlah.com/running-events-calendar-malaysia/page/7/> (referer: None)
--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
2018-02-26 14:13:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.justrunlah.com/running-events-calendar-malaysia/page/2/> (referer: None)
--------------------------------------------------
                SCRAPING DES ELEMENTS EVENTS
--------------------------------------------------
2018-02-26 14:13:22 [scrapy.core.engine] INFO: Closing spider (finished)
2018-02-26 14:13:22 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 6899,
 'downloader/request_count': 21,
 'downloader/request_method_count/GET': 21,
 'downloader/response_bytes': 380251,
 'downloader/response_count': 21,
 'downloader/response_status_count/200': 12,
 'downloader/response_status_count/301': 9,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2018, 2, 26, 13, 13, 22, 63002),
 'log_count/DEBUG': 22,
 'log_count/INFO': 7,
 'response_received_count': 12,
 'scheduler/dequeued': 20,
 'scheduler/dequeued/memory': 20,
 'scheduler/enqueued': 20,
 'scheduler/enqueued/memory': 20,
 'start_time': datetime.datetime(2018, 2, 26, 13, 13, 17, 308549)}
2018-02-26 14:13:22 [scrapy.core.engine] INFO: Spider closed (finished)

(C:\Users\guichet-v\AppData\Local\Continuum\anaconda3) C:\Users\guichet-v\Documents\CHALLENGE\02_TRAVAIL\ETAPE_1_WebToSGBD\SCRIPT\justrunlah>

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    正如@stranac 所说,问题来自 Xpath。目前,当我在 Google 控制台中复制元素的 Xpath 时,有一个 tbody 标签。但是这个标签不在源代码中。正如@gangabass 解释的here,这是“一个常见的问题:有时表格的源HTML 中没有tbody 标记(现代浏览器自动将其添加到DOM)”。我删除了它,提取工作,但它没有按我想要的方式组织(一个事件一行)我将所有提取数据放在一个单元格中。

    【讨论】:

      【解决方案2】:

      从浏览器的开发人员工具中复制元素 xpath 将为您提供仅匹配该 1 个元素的内容。
      即使这样,浏览器有时也需要修改 html 才能显示它,而且由于您的 xpath 是超级特定的,因此您可能连那 1 个匹配项都得不到。

      如何解决这个问题?

      看一下html,找到相关的元素、类和id,自己写一个xpath。
      例如,像//tr 这样简单的东西会匹配您尝试与//*[@id="td-outer-wrap"]/div[3]/div/div/div[1]/div/div[2]/div[3]/table/tbody/tr 匹配的所有元素。

      【讨论】:

      • 感谢您的帮助!其实它来自tbody标签,在源代码中实际上是不存在的。
      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多