Network programming for PyS60 (XIV)

by Marcelo Barros

Have you already heard about Beautiful Soup ? Beautiful Soup is a Python HTML/XML parser with a lot of useful methods to collect data from pages or for navigating, searching, and modifying a parse tree. In this post I will show some usage examples of BeautifulSoup and a link checker application for S60, combining BeautifulSoup and urllib.

Suppose you have a large (and confuse) HTML file. How about to show it totally organized and indented ? It is possible, with prettify() method. Just create a BeautifulSoup object and feed it with your HTML, like below:

from  BeautifulSoup import BeautifulSoup
html = u"""<html><body><h1 style="text-align:center">Heading 1</h1>
<p>Page content goes here.
<h2>And here.</h2></p><a href="" 
alt="Croozeus link">Croozeus</a><br/>
<a href="">Python</a><br/>
<h1>The end.</h1></body>
soup = BeautifulSoup(html)
print soup.prettify()

The output is below:

  <h1 style="text-align:center">
   Heading 1
   Page content goes here.
    And here.
  <a href="" alt="Croozeus link">
  <br />
  <a href="">
  <br />
   The end.

The parser tree is available with nice operations like findAll(). For instance, how about to print all links in the page ? Just use a dictionary with all tags you want as argument:

links = soup.findAll({'a':True})
for link in links:
    print "-->", link['href'].encode('utf-8')

The output is below:


Now, suppose you want to add the attribute alt to all links, modifying the parser tree. Simple:

for link in links:
    link['alt'] = link['href']
print soup.prettify()

The output is below:

  <a href="" alt="">
  <br />
  <a href="" alt="">

As you can see each link is like a dictionary and it is really straightforward to modify it.

The contents between <p> and </p> can be retrieved with find() and contents calls. find just looks for the first tag, not all, and contents is used by BeautifulSoup to store all the stuff for such tag. The result is an array of elements. In this case, the first is a string and the second is a new parseable element of the tree:

p = soup.find('p')
print p.contents
print p.contents[0]
print p.contents[1]
print p.contents[1].contents

The output is below:

[u'Page content goes here.\n', <h2>And here.</h2>]
Page content goes here.
<h2>And here.</h2>
[u'And here.']

There are a complete set of function to navigate through the tree. For instance, it is possible to start our search at first tag ‘p’ and after locating its child just with the following code:

p = soup.p
h2 = p.findChild()
print h2

The output is below:

<h2>And here.</h2>

Or start the search at first tag ‘h1’ but now looking for next siblings:

while h1:
    print h1
    h1 = h1.findNextSibling('h1')

The output is below:

<h1 style="text-align:center">Heading 1</h1>
<h1>The end.</h1>

Time to use this knowledge in a new application: a basic link checker for S60 devices. The idea is to download the contents of some URL and check all link inside it using BeautifulSoup and urllib. I had problems with some pages when running from mobile (Python for S60 1.9.5 only). Sometimes BeautifulSoup failed but the same pages worked when using Python 2.6. The code is below. The time required for detection a bad link could be decreased if the timeout argument could be set. However, PC version of urllib supports timeout but it is not available for PyS60.

Typing URL



# -*- coding: utf-8 -*-
# Marcelo Barros de Almeida
# License: GPL 3
import sys
    # Try to import 'btsocket' as 'socket' - ignored on versions < 1.9.x
    sys.modules['socket'] = __import__('btsocket')
except ImportError:
import socket
from BeautifulSoup import BeautifulSoup
import os
import e32
import urllib
import hashlib
from appuifw import *
class LCOpener(urllib.FancyURLopener):
    """ For mediawiki it is necessary to change the http agent.
    version = 'Mozilla/5.0'
class LinkChecker(object):
    def __init__(self):
        self.lock = e32.Ao_lock()
        self.dir = "e:\\linkchecker"
        if not os.path.isdir(self.dir):
        self.apo = None
        self.url = u''
        self.running = False
        app.title = u"Link Checker"
        app.screen = "normal" = [(u"Check URL",self.check_url),
                    (u"About", self.about),
                    (u"Exit", self.close_app)]
        self.body = Text()
        app.body = self.body
        self.lock = e32.Ao_lock()
    def close_app(self):
    def sel_access_point(self):
        """ Select and set the default access point.
            Return the access point object if the selection was done or None if not
        aps = socket.access_points()
        if not aps:
            note(u"No access points available","error")
            return None
        ap_labels = map(lambda x: x['name'], aps)
        item = popup_menu(ap_labels,u"Access points:")
        if item is None:
            return None
        apo = socket.access_point(aps[item]['iapid'])
        return apo
    def about(self):
        note(u"Link checker by Marcelo Barros (","info")
    def check_url(self):
        if self.running:
            note(u"There is a checking already in progress",u"info")
        self.running = True
        url = query(u"URL to check", "text", self.url)
        if url is not None:
            self.url = url
            self.apo = self.sel_access_point()
            if self.apo:
        self.running = False
    def run_checker(self):
        self.body.add(u"* Downloading page: %s ...\n" % self.url)
        fn = os.path.join(self.dir,'temp.html')
        except Exception, e:
        self.body.add(u"* Parsing links ...\n")
        page = open(fn,'rb').read()
            soup = BeautifulSoup(page)
            self.body.add(u"* BeautifulSoup error when decoding html. Aborted.")
        tags = soup.findAll({'img':True,'a':True})
        links = {}
        bad_links = []
        for n,tag in enumerate(tags):
            if tag.has_key('href'):
                link = tag['href']
            elif tag.has_key('img'):
                link = tag['src']
            # just check external links 
            if link.startswith(u'http'):
                # not handling internal links
                link = link.split(u'#')[0]
                # using a hash to avoid repeated links
                h = hashlib.md5()
                links[h.digest()] = link
        nl = len(links)
        for n,k in enumerate(links):
            link = links[k]
            msg = u"[%d/%d] Checking %s " % (n+1,nl,link)
            (valid,info) = self.check_link(link.encode('utf-8'))
            if valid:
                msg = u"==> Passed\n"
                msg = u"==> Failed: %s\n" % info
        msg = u"* Summary: %d links (%d failed)\n" % (nl,len(bad_links))
        for link in bad_links:
            self.body.add(u"==> %s failed\n" % link)
        self.body.add(u"* Finished")
    def check_link(self,link):
        """ Check if link (encoded in utf-8) exists.
            Return (True,'') or (False,'error message')
            page = LCOpener().open(link)
        except Exception, e:
            return (False,unicode(repr(e)))
           return (True,u'')
lc = LinkChecker()

Related posts:

  1. Network programming for PyS60 (X) To celebrate our tenth post, I will talk about urllib...
  2. Network programming for PyS60 (XIII) In our last post we talked about multicast, a special...
  3. Network programming for PyS60 (VI) Before presenting some server code it is important to discuss...
  4. Network programming for PyS60 (VII) Everything is about "protocols" in computer networks, doesn't it ?...
  5. Network programming for PyS60 (VIII) Did you do your homework ? So, I would like...

Related posts brought to you by Yet Another Related Posts Plugin.