Posted By

dugo on 04/18/12


Tagged

pythoncssinlineextractorextracthtmlclean


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

peterbenoit


python css inline extractor


 / Published in: Python
 

URL: python-css-inline-extractor

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

Only BeautifulSoup installed.

  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()

Report this snippet  

You need to login to post a comment.