Naudotojas:Powermelon/smėlio dėžė

Puslapis iš Vikipedijos, laisvosios enciklopedijos.

Programa, analizuojanti ELIP {{dem}} lenteles:

from urllib.request import urlopen
import fileinput
import re

def decode_entities(match):
    return chr( int( match.group(1) ) )

response = urlopen( 'http://lietuvai.lt/w/index.php?title=U%C5%BEulaukis_(Ukmerg%C4%97)&action=formedit' )
text = response.read().decode('utf-8')
text = re.sub( r'\&#(\d+);', decode_entities, text )

match = re.search( r'\{\{(\s*dem\s*\|[^}]+)\}\}', text )
dem = match.group(1)

table = dict()
year = None

for field in dem.split( '|' ):
    field = re.sub( r'\s', '', field )
    if field == 'dem' or re.search( r'=', field ) is not None:
        continue
    if year is None:
        year = field
    else:
        table[year] = field
        year = None

print(table)