Return to Snippet

Revision: 56827
at April 18, 2012 01:13 by dugo


Initial Code
#!/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()

Initial URL
python-css-inline-extractor

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

Only BeautifulSoup installed.

Initial Title
python css inline extractor

Initial Tags

                                

Initial Language
Python