get-billboard.py
                        
                             · 1.0 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            #!/usr/bin/env python2
from bs4 import BeautifulSoup as Soup
import requests
from collections import namedtuple
from datetime import datetime, timedelta
Song = namedtuple('Song', ['title', 'artist', 'position', 'spotify', 'vevo', 'rdio'])
def billboard(date):
    soup = Soup(requests.get("http://www.billboard.com/charts/hot-100/%s" % date).text)
    songs = soup.findAll('article', {'class': 'chart-row'})
    for item in songs:
        title = item.find('h2').text.strip()
        artist = item.find('h3').text.strip()
        position = item.find('span', {'class': 'this-week'}).text.strip()
        _ = item.find('a', {'class': 'spotify'})
        spotify = _["href"].split("uri=")[1] if _ else None
        _ = item.find('a', {'title': 'Vevo'})
        vevo = _["href"].split("video=")[1] if _ else None
        _ = item.find('a', {'class': 'rdio'})
        rdio = _['href'] if _ else None
        yield Song(title, artist, position, spotify, vevo, rdio)
if __name__ == "__main__":
    for song in billboard('2015-06-06'):
        print song
                | 1 | #!/usr/bin/env python2 | 
| 2 | from bs4 import BeautifulSoup as Soup | 
| 3 | import requests | 
| 4 | from collections import namedtuple | 
| 5 | from datetime import datetime, timedelta | 
| 6 | |
| 7 | Song = namedtuple('Song', ['title', 'artist', 'position', 'spotify', 'vevo', 'rdio']) | 
| 8 | |
| 9 | def billboard(date): | 
| 10 | soup = Soup(requests.get("http://www.billboard.com/charts/hot-100/%s" % date).text) | 
| 11 | songs = soup.findAll('article', {'class': 'chart-row'}) | 
| 12 | for item in songs: | 
| 13 | title = item.find('h2').text.strip() | 
| 14 | artist = item.find('h3').text.strip() | 
| 15 | position = item.find('span', {'class': 'this-week'}).text.strip() | 
| 16 | _ = item.find('a', {'class': 'spotify'}) | 
| 17 | spotify = _["href"].split("uri=")[1] if _ else None | 
| 18 | _ = item.find('a', {'title': 'Vevo'}) | 
| 19 | vevo = _["href"].split("video=")[1] if _ else None | 
| 20 | _ = item.find('a', {'class': 'rdio'}) | 
| 21 | rdio = _['href'] if _ else None | 
| 22 | yield Song(title, artist, position, spotify, vevo, rdio) | 
| 23 | |
| 24 | if __name__ == "__main__": | 
| 25 | for song in billboard('2015-06-06'): | 
| 26 | print song |