值我有HTML:提取属性的使用XPath在Python
<table>
<tbody>
<tr>
<td align="left" valign="top" style="padding: 0 10px 0 60px;">
<img src="/files/39.jpg" width="64" height="64">
</td>
<td align="left" valign="middle"><h1>30 Rock</h1></td>
</tr>
</tbody>
</table>
使用Python和LXML我需要从<img>
元素的属性src
提取价值。以下是我已经试过:
import lxml.html
import urllib
# make HTTP request to site
page = urllib.urlopen("http://my.url.com")
# read the downloaded page
doc = lxml.html.document_fromstring(page.read())
txt1 = doc.xpath('/html/body/table[2]/tbody/tr/td[1]/img')
当我打印txt1
我得到空列表仅[]
。我该如何解决这个问题?
使用'img/@ src'。至于为什么你得到空列表,如果这是你正在解析的实际HTML,那么这是可以预料的,因为它与XPath不匹配。 – 2012-07-27 21:32:55
感谢您的咨询。这只是HTML-doc的一个缩减。 – 2012-07-28 05:53:57