python:BeautifulSoup形式美化html

528 ワード

htmlPretty.py
from BeautifulSoup import BeautifulSoup
import os, sys 

if len(sys.argv) ==2:
    f1 = sys.argv[1]
else:
    print 'usage: htmlPretty.py file1.htm '
    sys.exit(1)

if not os.path.exists(f1):
    print 'ERROR: %s not found
' % f1 sys.exit(1) fp = open(f1, 'r') soup = BeautifulSoup(fp, fromEncoding='utf-8') # or cp936 fp.close() f2 = f1 +'l' fp = open(f2, 'w') fp.write(soup.prettify('utf-8')) fp.close() print f2