Prettify working using python idle but not when using python script
Posted
by
Loclip
on Stack Overflow
See other posts from Stack Overflow
or by Loclip
Published on 2012-11-10T09:42:48Z
Indexed on
2012/11/10
11:01 UTC
Read the original article
Hit count: 275
python
|beautifulsoup
So when I use print soup.prettify()
on idle it's prettifying my html but when I calling the script from server the prettify()
not working
#!/usr/bin/python
import cgi, cgitb, urllib2, sys
from bs4 import BeautifulSoup
styles = []
i = 1
site = "www.example.com"
page = urllib2.urlopen(site)
soup = BeautifulSoup(page)
table = soup.find('table', {'class' :'style5'})
alltd = table.findAll('td')
allspans = table.findAll('span')
while i<55:
styles.append("style" + str(i))
i+=1
for td in alltd:
if any(x in td["class"] for x in styles):
td["class"] = "align"
for span in allspans:
span.replaceWithChildren()
print "Content-type: text/html"
print
print "<!DOCTYPE html>"
print "<html>"
print "<head>"
print '<meta http-equiv="content-type" content="text/html; charset=utf-8">'
print '<link rel="stylesheet" href="lightbox/css/lightbox.css" type="text/css" media="screen">'
print '<style type="text/css">'
print "body {background-color:#b0c4de;}"
print '.align {text-align: center;}'
print "</style>"
print "</head>"
print "<body>"
print table.pretiffy()
print "</body>"
print "</html>"
© Stack Overflow or respective owner