I need a regex for the href attribute for an mp3 file url in python
Question: Based on a previous stack overflow question and contribution by cgoldberg, I
came up with this regex using the python re module:

    
    
    import re
    urls = re.finditer('http://(.*?).mp3', htmlcode)
    

The variable urls is an iterable object and I can use a loop to access each
mp3 file url individually if there is more than one :

    
    
    for url in urls:
        mp3fileurl = url.group(0)
    

This technique, however, only works sometimes. I realize regular expressions
will not be as reliable as a fully fledged parser module. But, sometimes, this
is not reliable for the same page.

I sometimes receive everything before http for some url entries.

I am relatively new to regular expressions. So, I am just wondering if there
is a more reliable way to go about it.

Thanks in advance. New to stackoverflow and looking forward to contributing
some answers as well.


Answer: As pointed out by the other answers, using regular expressions to parse HTML =
bad, bad idea.

With that in mind, I will add in code of my favorite parser:
[BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/):

    
    
    from BeautifulSoup import BeautifulSoup
    
    soup = BeautifulSoup(htmlcode)
    links = soup.findAll('a', href=True)
    mp3s = [l for l in links if l['href'].endswith('.mp3')]
    for song in mp3s:
        print link['href']
    

