使用XPath获取某些属性值

khe*_*lll 9 python xpath

从以下HTML代码段:

<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" />
Run Code Online (Sandbox Code Playgroud)

我试图获得href的价值link标签与相对值= "shortcut icon",我试图做到这一点使用XPath.

如何在Python中做到这一点?

Mat*_*ttH 16

像这样:

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']
Run Code Online (Sandbox Code Playgroud)