BeautifulSoup: Last.fm Artist Image

A coworker had been working with Last.fm and libnotify to send him a notification when a friend listens to a song so that he can decide if he’d be interested in listening to it. To grab the artist’s image he used a complex regular expression that was prone to error. I thought it was a neat idea so I decided to implement for my stream. The result can be found here.

My implementation is a one liner:

event.image = soup.find('div', {'class': 'imgHolder'}).find('img').get('src')

Basically that says “find the div that has a class imgHolder, find the image inside of that div, and get me the link to the image”. BeautifulSoup makes things like this extremely easy. I highly recommend using it whenever you need to screen scrape.

Trackbacks

close Reblog this comment
blog comments powered by Disqus