This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 2nd, 2010  |  syntax: Python  |  size: 0.55 KB  |  views: 262  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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()
clone this paste RAW Paste Data