2010-02-11 7 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" /> 

내가 확인해 값 = "shortcut icon"link 태그의 href 값을 얻기 위해 노력하고있어 나는 XPath를 사용하는 것을 달성하기 위해 노력하고있어 .

어떻게 파이썬에서 그렇게합니까? 이처럼

답변

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'] 
관련 문제