Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting certain attribute value using XPath

Tags:

python

xpath

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 value = "shortcut icon", I'm trying to achieve that using XPath.

How to do that in Python?

like image 949
khelll Avatar asked Feb 11 '10 08:02

khelll


1 Answers

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']
like image 182
MattH Avatar answered Oct 24 '22 22:10

MattH