使用Scrapy XPATH获取属性的名称

Fac*_*bre 3 python xpath scrapy

我正在尝试在XML文件中获取某些标记的属性键和值(使用scrapy和xpath).

标签是这样的:

<element attr1="value1" attr2="value2 ...>
Run Code Online (Sandbox Code Playgroud)

我不知道键"attr1","attr2"等等,它们可以在两个元素之间切换.我没弄明白如何用xpath获取键和值,还有其他好的做法吗?

pau*_*rth 8

精简版

>>> for element in selector.xpath('//element'):
...     attributes = []
...     # loop over all attribute nodes of the element
...     for index, attribute in enumerate(element.xpath('@*'), start=1):
...         # use XPath's name() string function on each attribute,
...         # using their position
...         attribute_name = element.xpath('name(@*[%d])' % index).extract_first()
...         # Scrapy's extract() on an attribute returns its value
...         attributes.append((attribute_name, attribute.extract()))
... 
>>> attributes # list of (attribute name, attribute value) tuples
[(u'attr1', u'value1'), (u'attr2', u'value2')]
>>> dict(attributes)
{u'attr2': u'value2', u'attr1': u'value1'}
>>> 
Run Code Online (Sandbox Code Playgroud)

长版

XPath具有获取节点名称的name(node-set?)功能(属性是节点,属性节点):

名称功能返回包含表示在参数节点集是第一按文档顺序的节点的扩展名是QName的字符串.(...)如果省略了参数,则默认为一个节点集合与上下文节点作为其唯一成员.

(来源:http://www.w3.org/TR/xpath/#function-name)

>>> import scrapy
>>> selector = scrapy.Selector(text='''
...     <html>
...     <element attr1="value1" attr2="value2">some text</element>
...     </html>''')
>>> selector.xpath('//element').xpath('name()').extract()
[u'element']
Run Code Online (Sandbox Code Playgroud)

(这里,我链接name()//element选择的结果,将函数应用于所有选定的元素节点.Scrapy选择器的一个方便的功能)

一个人想对属性节点做同样的事,对吧?但它不起作用:

>>> selector.xpath('//element/@*').extract()
[u'value1', u'value2']
>>> selector.xpath('//element/@*').xpath('name()').extract()
[]
>>> 
Run Code Online (Sandbox Code Playgroud)

注意:我不知道它是否是lxml/libxml2Scrapy在引擎盖下使用的限制,或者XPath规范是否禁止它.(我不明白为什么会这样.)

你可以做的是使用name(node-set)表单,即使用非空节点集作为参数.如果仔细阅读上面粘贴的XPath 1.0规范部分,与其他字符串函数一样,name(node-set)只考虑节点集中的第一个节点(按文档顺序):

>>> selector.xpath('//element').xpath('@*').extract()
[u'value1', u'value2']
>>> selector.xpath('//element').xpath('name(@*)').extract()
[u'attr1']
>>> 
Run Code Online (Sandbox Code Playgroud)

属性节点也有位置,因此您可以按位置循环所有属性.这里我们有2个(count(@*)上下文节点的结果):

>>> for element in selector.xpath('//element'):
...     print element.xpath('count(@*)').extract_first()
... 
2.0
>>> for element in selector.xpath('//element'):
...     for i in range(1, 2+1):
...         print element.xpath('@*[%d]' % i).extract_first()
... 
value1
value2
>>> 
Run Code Online (Sandbox Code Playgroud)

现在,你可以猜到我们能做些什么:name()为每个人打电话@*[i]

>>> for element in selector.xpath('//element'):
...     for i in range(1, 2+1):
...         print element.xpath('name(@*[%d])' % i).extract_first()
... 
attr1
attr2
>>> 
Run Code Online (Sandbox Code Playgroud)

如果你将所有这些放在一起,并假设@*它将按文档顺序获取属性(我认为不是在XPath 1.0规范中说的,但这是我看到的情况lxml),你最终得到这个:

>>> attributes = []
>>> for element in selector.xpath('//element'):
...     for index, attribute in enumerate(element.xpath('@*'), start=1):
...         attribute_name = element.xpath('name(@*[%d])' % index).extract_first()
...         attributes.append((attribute_name, attribute.extract()))
... 
>>> attributes
[(u'attr1', u'value1'), (u'attr2', u'value2')]
>>> dict(attributes)
{u'attr2': u'value2', u'attr1': u'value1'}
>>> 
Run Code Online (Sandbox Code Playgroud)