【问题标题】:How to scrape and parse nested div with scrapy如何使用 scrapy 抓取和解析嵌套的 div
【发布时间】:2016-11-18 23:12:07
【问题描述】:

尝试关注此 github 页面以学习在 facebook 中抓取嵌套 div。 https://github.com/talhashraf/major-scrapy-spiders/blob/master/mss/spiders/facebook_profile.py

文件中的parse_info_text_onlyparse_info_has_image 可以正常获取跨度信息

我有一个类似的页面,我试图从嵌套的 div 中获取 result_id,但是 result_id 在 div 本身中。

据我了解,我试图 scrape 的 div 位于第二行,所以我尝试类似

def parse_profile(self, response):
       item["BrowseResultsContainer"] = self.parse_info_has_id(response.css('#BrowseResultsContainer'))
return item

def parse_info_has_id(self, css_path):
           text = css_path.xpath('div/div').extract()
           text = [t.strip() for t in text]
           text = [t for t in text if re.search('result_id', t)]
           return "\n".join(text)

如何从嵌套 div 上方获取 data-xt?

【问题讨论】:

    标签: html parsing scrapy web-crawler


    【解决方案1】:

    用css:

    import json
    ...
        def parse_info_has_id(self, css_path):
            text = css_path.xpath('div::attr(data-gt)').extract_first()
            d = json.loads(text)
            return d['result_id']
    

    【讨论】:

      【解决方案2】:

      我想,如果你想要所有数据-xt 那么

      def parse_info_has_id(self, css_path):
             text = css_path.xpath('//div[@data-xt != ""]').extract()
             text = [t.strip() for t in text]
             text = [t for t in text if re.search('result_id', t)]
             return "\n".join(text)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-10
        • 2013-02-17
        • 2019-11-05
        • 2016-04-27
        相关资源
        最近更新 更多