【问题标题】:Parsing sections delimited by tags解析由标签分隔的部分
【发布时间】:2014-08-12 08:03:08
【问题描述】:

我需要对由标题分隔的元素进行 bin 处理。我正在努力制定一个 xpath 表达式或简单的解析器,它可以将我的项目分组到标题标签给出的部分中。

我了解如何抓取元素位于同一级别或元素级别由容器给出的列表,但我正在努力弄清楚如何解析容器由元素分隔的数据。例如:

<div>
<h1>section a</h1>
<item>221</item>
<item>453</item>
<item>473</item>
<h1>section b</h1>
<item>430</item>
<item>493</item>
<h1>section c</h1>
<item>694</item>
<item>931</item>
</div>

是否有一些典型的方式来使用 xpath 来记录结构?有没有办法遍历scrapy选择器,以便我看到一个dom视图并检测这些部分的开始和停止?

【问题讨论】:

标签: python xpath web-scraping scrapy


【解决方案1】:

使用 XPath 的一种解决方案是计算 div 下的节点的前面 h1 兄弟姐妹,这些节点本身不是 h1

$ ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import scrapy

In [2]: selector = scrapy.Selector(text="""
<div>
<h1>section a</h1>
<item>221</item>
<item>453</item>
<item>473</item>
<h1>section b</h1>
<item>430</item>
<item>493</item>
<h1>section c</h1>
<item>694</item>
<item>931</item>
</div>""")

In [3]: for i, header in enumerate(selector.xpath('.//div/h1'), start=1):
    print header.xpath('normalize-space()').extract()
    between = selector.xpath(""".//div/node()[count(preceding-sibling::h1)=%d]
                                             [not(self::h1)]""" % i)
    print between.extract()
   ...:     
[u'section a']
[u'\n', u'<item>221</item>', u'\n', u'<item>453</item>', u'\n', u'<item>473</item>', u'\n']
[u'section b']
[u'\n', u'<item>430</item>', u'\n', u'<item>493</item>', u'\n']
[u'section c']
[u'\n', u'<item>694</item>', u'\n', u'<item>931</item>', u'\n']

【讨论】:

    【解决方案2】:
    var header = null
    var items = []
    
    for each element in div
        if element is header
            process previous header, items
            header = the element text
            items = []
        else
            items append element text
    end
    process last header, items
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 2015-09-13
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多