Python: Prosty parser z wykorzystaniem bilbioteki BeautifulSoup w PSP (Python Server Pages)

Dziś prezentuję prosty skrypt parsujący stronę popularnego radia i wyciągający listę 20 najgorętszych hitów. Skrypt wykorzystuje świetną bibliotekę do parsowania HTML/XHTML, mianowicie BeautifulSoup. Skrypcik napisany w Pythonie / PSP (Python Server Pages).

eskaTop20.psp:

<%
# -*- coding: utf-8 -*-
from BeautifulSoup import BeautifulSoup
from urllib import urlopen
 
url = urlopen('http://www.eska.pl/goraca20')
soup = BeautifulSoup(url.read())
titleTag = soup.html.head.title
req.write(str(titleTag.string)+"<br/><br/>")
 
lista = soup.find('div', attrs={'class': 'lista'})
l = lista.findAll('div', attrs={'class': 'name'})
for e in l:
    # artist
    spanFirst = e.find('span', attrs={'class': 'first'});
    x = spanFirst.findAll('a')
    j = 0
    for i in x:
        j += 1
        req.write('<span style="font-weight: bold;">'+str(i.string.strip())+'</span>'),
        if len(x) > 1 and j < len(x):
            req.write(','),
    # title
    req.write(" - " + str(spanFirst.findNextSibling('span').find('a').string).strip() + '<br/>')
 
%>

.htaccess:

# Python Server Pages
AddHandler mod_python .psp
PythonHandler mod_python.psp

Działający skrypt można obejrzeć tutaj:
http://marioosh.net/psp/eskaTop20.psp

Podobne – topLista RMF, Liga hitów RadiaZet:
http://marioosh.net/psp/topListaRMF.psp
http://marioosh.net/psp/ligaHitow.psp

Leave a Reply