使用xlwt库将爬取内容写入excel

#coding=UTF-8
import urllib2
from bs4 import BeautifulSoup
import sys
import xlwt
page=1
num=0
book=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet=book.add_sheet('test',cell_overwrite_ok=True)
while page<100:
pages = str(page)
url = "http://ty.fangjia.com/zufang/--e-" + pages + "#pagelist"
reload(sys)
sys.setdefaultencoding('utf-8')
html = urllib2.urlopen(url)
soup = BeautifulSoup(html, "html.parser")
house = soup.find("div", class_="house")
for home in house.find_all("li", {"name": "__page_click_area"}):
for title in home.find_all("span", class_="tit"):
for text in title.stripped_strings:
sheet.write(num+1, 0, text)
for address in home.find_all("span", class_="address"):
i=1
for text in address.stripped_strings:
sheet.write(num + 1, i, text)
i=i+1
for attribute in home.find_all("span", class_="attribute"):
for text in attribute.stripped_strings:
sheet.write(num + 1, 3, text)
for price in home.find_all("span", class_="xq_aprice xq_esf_width"):
for text in price.em.stripped_strings:
sheet.write(num + 1, 4, text+ "万")
num=num+1
print str(page) + "页插入完成"
book.save('/Users/dubo/Desktop/typrice2.xls')
page = page + 1
print "总计"+str(num)+"套房屋"