github.com/hoffmann Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

I need a regex for the href attribute for an mp3 file url in python

Posted on May 5, 2009
#stackoverflow #python

This my Answer to the stackoverflow question: I need a regex for the href attribute for an mp3 file url in python:

As always I suggest using a html parser like lxml.html instead of regular expressions to extract informations from html files:

import lxml.html

tree = lxml.html.fromstring(htmlcode)
for link in tree.findall(".//a"):
    url = link.get("href")
    if url.endswith(".mp3"):
        print url