2010-02-11 21 views
8

Desde el siguiente fragmento de HTML:conseguir cierto valor de atributo mediante 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" /> 

Estoy tratando de obtener el valor de la etiqueta hreflink con valor rel = "shortcut icon", yo estoy tratando de lograr que el uso de XPath .

¿Cómo hacer eso en Python?

Respuesta

15

De esta manera:

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