BeautifulSouping Some Movie Recommendations

There was a link on Metafilter a while back to a bunch of movie directors and their top ten movie picks (over at I found some good ones on there, but kinda wanted a big ol' aggregate list, so I python'd one up. I'm just giving them points according to how high they are on a given list and adding those up. List and codes follow:

from bs4 import BeautifulSoup
import urllib
from collections import defaultdict

def get_things():
    soup = BeautifulSoup(urllib.urlopen(''))
    rows = soup.find_all('table')
    result = []
    for r in rows:
        children = list(r.children)
        if len(children) == 5:
            kidz = children[3].find_all('i')
            result.append((children[1].find('font'), [(tag.string, len(kidz) - (i)) for i, tag in enumerate(kidz)]))
    return result

if __name__ == "__main__":
    things = get_things()
    aggregate = defaultdict(int)
    for thing, films in things:
        for film, score in films:
            aggregate[film] += score

    sorts = sorted(aggregate.items(), cmp=lambda x,y:cmp(x[1], y[1]))[::-1]

    print "\n".join(["%s : %d" % (k,v) for k, v in sorts])

Continue reading "BeautifulSouping Some Movie Recommendations"