2010-02-11 69 views
8

從下面的HTML代碼段獲得一定的屬性值:使用XPath

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

我試圖讓link標記與相對值= "shortcut icon"href價值,我試圖做到這一點使用XPath 。

如何在Python中做到這一點?

回答

15

喜歡分享:

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