Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, XPath: Find all links to images

Tags:

python

xpath

lxml

I'm using lxml in Python to parse some HTML and I want to extract all link to images. The way I do it right now is:

//a[contains(@href,'.jpg') or contains(@href,'.jpeg') or ... (etc)]

There are a couple of problem with this approach:

  • you have to list all possible image extensions in all cases (both "jpg" and "JPG"), wich is not elegant
  • in a weird situations, the href may contain .jpg somewhere in the middle, not at the end of the string

I wanted to use regexp, but I failed:

//a[regx:match(@href,'.*\.(?:png|jpg|jpeg)')]

This returned me all links all the time ...

Does anyone knows the right, elegant way to do this or what is wrong with my regexp approach ?

like image 314
Nicu Surdu Avatar asked Dec 01 '10 21:12

Nicu Surdu


4 Answers

Instead of:

a[contains(@href,'.jpg')]

Use:

a[substring(@href, string-length(@href)-3)='.jpg']

(and the same expression pattern for the other possible endings).

The above expression is the XPath 1.0 equivalent to the following XPath 2.0 expression:

a[ends-with(@href, '.jpg')]
like image 172
Dimitre Novatchev Avatar answered Oct 30 '22 15:10

Dimitre Novatchev


Use XPath to return all <a> elements and use a Python list comprehension to filter down to those matching your regex.

like image 36
Marcelo Cantos Avatar answered Oct 30 '22 14:10

Marcelo Cantos


lxml supports regular expressions in EXSLT namespace:

from lxml import html

# download & parse web page
doc = html.parse('http://apod.nasa.gov/apod/astropix.html')

# find the first <a href that ends with .png or .jpg or .jpeg ignoring case
ns = {'re': "http://exslt.org/regular-expressions"}
img_url = doc.xpath(r"//a[re:test(@href, '\.(?:png|jpg|jpeg)', 'i')]/@href",
                    namespaces=ns, smart_strings=False)[0]
print(img_url)
like image 2
jfs Avatar answered Oct 30 '22 15:10

jfs


Because there's no guarantee that the link has a file extension at all, or that the file extension even matches the content (.jpg URLs returning error HTML, for example) that limits your options.

The only correct way to gather all images from a site would be to get every link and query it with an HTTP HEAD request to find out what Content-type the server is sending for it. If the content type is image/(anything) it's an image, otherwise it's not.

Scraping the URLs for common file extensions is probably going to get you 99.9% of images though. It's not elegant, but neither is most HTML. I recommend being happy to settle for 99.9% in this case. The extra 0.1% isn't worth it.

like image 1
cecilkorik Avatar answered Oct 30 '22 13:10

cecilkorik