SHARE
TWEET

Untitled

a guest Nov 2nd, 2010 371 Never
  1. from BeautifulSoup import BeautifulSoup
  2.  
  3. doc = '''<html><head><title>Page title</title></head>
  4.       <body>
  5.       <div>
  6.       <a href="..." title="text to highlight">Some text to highlight</a>
  7.       <a href="..." title="text to highlight">Some other text to highlight</a>
  8.       </div>
  9.       </body></html>'''
  10.  
  11. def transformText(original):
  12.     return original + '!'
  13.  
  14.  
  15.  
  16.  
  17. soup = BeautifulSoup(doc)
  18.  
  19. for div in soup.findAll('div'):
  20.     for link in div.findAll('a'):
  21.         link['title'] = transformText(link['title'])
  22.  
  23. print soup.prettify()
RAW Paste Data
Top