python css inline extractor


/ Published in: Python
Save to your folder(s)

A python script to extract inline CSS in HTML made with some graphical tool (like iWeb).

Only BeautifulSoup installed.


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. from BeautifulSoup import BeautifulSoup as BS
  4.  
  5. import sys
  6.  
  7. bs = BS(open(sys.argv[1]).read())
  8.  
  9. count = 0
  10. styles = []
  11. for t in bs.findAll(True):
  12.  
  13. if t.get('style'):
  14.  
  15. myclass = "extracted_%d" % count
  16. styles.append( ".%s { %s }" % (myclass,t.get('style'),))
  17.  
  18. del t['style']
  19.  
  20. # has class
  21. if t.get('class'):
  22. t['class'] = t['class'] + " "+ myclass
  23. # no has class
  24. else:
  25. t['class'] = myclass
  26.  
  27. count+=1
  28.  
  29. print "\n".join(styles)
  30. print bs.prettify()

URL: python-css-inline-extractor

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.