使用 XPath 获取特定属性值 [英] Getting certain attribute value using XPath
本文介绍了使用 XPath 获取特定属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自以下 HTML 片段:
From the following HTML snippet:
<link rel="index" href="/index.php" />
<link rel="contents" href="/getdata.php" />
<link rel="copyright" href="/blabla.php" />
<link rel="shortcut icon" href="/img/all/favicon.ico" />
我正在尝试使用 rel value = "shortcut icon"
获取 link
标记的 href
值,我正在尝试使用 XPath 实现这一点.
I'm trying to get the href
value of the link
tag with rel value = "shortcut icon"
, I'm trying to achieve that using XPath.
如何在 Python 中做到这一点?
How to do that in Python?
推荐答案
像这样:
data = """<link rel="index" href="/index.php" />
<link rel="contents" href="/getdata.php" />
<link rel="copyright" href="/blabla.php" />
<link rel="shortcut icon" href="/img/all/favicon.ico" />
"""
from lxml import etree
d = etree.HTML(data)
d.xpath('//link[@rel="shortcut icon"]/@href')
['/img/all/favicon.ico']
这篇关于使用 XPath 获取特定属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文