/ Published in: Python
A python script to extract inline CSS in HTML made with some graphical tool (like iWeb).
Only BeautifulSoup installed.
Only BeautifulSoup installed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/python from BeautifulSoup import BeautifulSoup as BS import sys bs = BS(open(sys.argv[1]).read()) count = 0 styles = [] for t in bs.findAll(True): if t.get('style'): myclass = "extracted_%d" % count styles.append( ".%s { %s }" % (myclass,t.get('style'),)) del t['style'] # has class if t.get('class'): t['class'] = t['class'] + " "+ myclass # no has class else: t['class'] = myclass count+=1 print "\n".join(styles) print bs.prettify()
URL: python-css-inline-extractor