Return to Snippet

Revision: 34170
at October 19, 2010 00:23 by stavelin


Initial Code
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8") # dette funker jo..!
from BeautifulSoup import BeautifulSoup
import urllib, re, time

url = 'http://www.vg.no/nyheter/innenriks/norsk-politikk/folkevalgte/'
html = urllib.urlopen(url).read()
suppe = BeautifulSoup(html)
table = suppe.findAll('a', href=re.compile('.*?kid=.*'))

for i in table:
    time.sleep(0.5) # don't panic!
    kommune_id = i['href'].split("=")
    kommune_id = kommune_id[1]
    if len(kommune_id) == 3:
        kommune_id = "0" + kommune_id
    kommune = i.text
    print '<li><a href="http://www.vg.no/nyheter/innenriks/norsk-politikk/folkevalgte/api/?kid=%s&format=xml">%s xml</a>, <a href="http://www.vg.no/nyheter/innenriks/norsk-politikk/folkevalgte/api/?kid=%s&format=json">json</a></li>' % (kommune_id, kommune, kommune_id)

Initial URL
http://stavelin.com/uib/data/

Initial Description


Initial Title
List of elected representatives in the Norwegian municipalities - VG scraper

Initial Tags
python

Initial Language
Python