我在 xpath 的帮助下提取了以下内容:
In [206]: list = tree.xpath('/html/body/div[@id="gs_top"]/div[@id="gs_bdy"]/div[@id="gs_ccl"]/div[@id="gsc_ccl"]/div[@class="gsc_1usr gs_scl"]/div[@class="gsc_1usr_text"]/h3[@class="gsc_1usr_name"]/a')
In [208]: for item in list:
print(etree.tostring(item, pretty_print=True))
.....:
<a href="/citations?user=lMkTx0EAAAAJ&hl=en&oe=ASCII">Jason Weston</a>
<a href="/citations?user=RhFhIIgAAAAJ&hl=en&oe=ASCII">Pierre Baldi</a>
<a href="/citations?user=9DXQi8gAAAAJ&hl=en&oe=ASCII">Yair Weiss</a>
<a href="/citations?user=J8YyZugAAAAJ&hl=en&oe=ASCII">Peter Belhumeur</a>
<a href="/citations?user=ORr4XJYAAAAJ&hl=en&oe=ASCII">Serge Belongie</a>
现在我可以通过附加 /@href
来提取 href,也可以在 text()
的帮助下提取文本。但我怎样才能一次性获得它们,如这里的答案所示:How to select two attributes from the same node with one expression in XPath?
最佳答案
只需这样对每个元素调用 .xpath("@href|text()")
即可:
for item in list:
href, text = item.xpath("@href|text()")
print(href, text)
演示:
>>> from lxml.html import fromstring
>>>
>>> data = """
... <body>
... <a href="/citations?user=lMkTx0EAAAAJ&hl=en&oe=ASCII">Jason Weston</a>
... <a href="/citations?user=RhFhIIgAAAAJ&hl=en&oe=ASCII">Pierre Baldi</a>
... <a href="/citations?user=9DXQi8gAAAAJ&hl=en&oe=ASCII">Yair Weiss</a>
... <a href="/citations?user=J8YyZugAAAAJ&hl=en&oe=ASCII">Peter Belhumeur</a>
... <a href="/citations?user=ORr4XJYAAAAJ&hl=en&oe=ASCII">Serge Belongie</a>
... </body>
... """
>>>
>>> tree = fromstring(data)
>>>
>>> for item in tree.xpath("//a"):
... print(item.xpath("@href|text()"))
...
['/citations?user=lMkTx0EAAAAJ&hl=en&oe=ASCII', 'Jason Weston']
['/citations?user=RhFhIIgAAAAJ&hl=en&oe=ASCII', 'Pierre Baldi']
['/citations?user=9DXQi8gAAAAJ&hl=en&oe=ASCII', 'Yair Weiss']
['/citations?user=J8YyZugAAAAJ&hl=en&oe=ASCII', 'Peter Belhumeur']
['/citations?user=ORr4XJYAAAAJ&hl=en&oe=ASCII', 'Serge Belongie']
关于python - 从 anchor 标记的 xpath 节点获取 2 属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468751/