Extract attribute's value with XPath in Python

Question:

I have the HTML:

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

Using Python and LXML I need to extract the value from the attribute src of the <img> element. Here’s what I’ve tried:

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

When I print txt1 I get the empty list only []. How can I correct this?

Asked By: Eugene Shmorgun

||

Answers:

Use this XPath:

//img/@src

Selects the src attributes of all img elements in the entire input XML document

Answered By: Kirill Polishchuk
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.