Monday, 3 December 2007

Markup exercise 12 using cached file

""" Markup Example 12 local

Adding a user-agent string to emulate a Nokia 6230
Reads the content from a cached file

author: Julian Harty
edited: 03 December 2007
import re
import amara

def getHrefFromXML(doc, search_regex):
  """Returns the href link if the in search_regex is
  found in any div tags.

  Assumes the links are in the html body's div tags.

    doc: an amara xml object
    search_regex: the regular expression to match in
      the href text

    the href as a string if the pattern is found, else None.
  ru1 = re.compile(search_regex)
  for item in doc.html.body.div:
      # print str(item.a.xml_children[0])
      # print type(item.a.xml_children[0])
      p =[0])
      if p:
        return item.a.href

  return None

if __name__ == "__main__":
  # use a local copy of the content
  doc = amara.parse(open("mobile-homepage.xhtml"))

  print "should return: '/gmm?source=m&dc=mobile-promotion'"
  print getHrefFromXML(doc, "Maps")