开发者

Getting certain attribute value using XPath

开发者 https://www.devze.com 2022-12-19 17:09 出处:网络
From the following HTML snippet: <link rel=\"index\" href=\"/index.php\" /> <link rel=\"contents\" href=\"/getdata.php\" />

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" />

I'm trying to get the href value of the link tag with rel val开发者_开发知识库ue = "shortcut icon", I'm trying to achieve that using XPath.

How to do that in Python?


Like this:

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']
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号