python : BeautifulSoup 格式美化 html

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\n' % 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())
fp.close()
print f2

猜你喜欢

转载自blog.csdn.net/belldeep/article/details/81837171