将结果写入.xls(将2个查询提交到网页,并将不同的结果存储到.xls中)

大家好…我正在使用Python 2.76将查询提交到.aspx网页,并通过BeautifulSoup获取结果,并且希望将它们存储到Excel电子表格中。

import mechanize import re import xlwt from bs4 import BeautifulSoup import urllib2 book = xlwt.Workbook(encoding='utf-8', style_compression = 0) sheet = book.add_sheet('Legi', cell_overwrite_ok = True) for items in ['university student', 'high school student']: url = r'http://legistar.council.nyc.gov/Legislation.aspx' request = mechanize.Request(url) response = mechanize.urlopen(request) forms = mechanize.ParseResponse(response, backwards_compat=False) form = forms[0] response.close() form['ctl00$ContentPlaceHolder1$txtSearch'] = items submit_page = mechanize.urlopen(form.click()) soup = BeautifulSoup(submit_page.read()) aa = soup.find_all(href=re.compile('LegislationDetail')) for bb in aa: cc = bb.text #print cc results = [] results.append(cc) for row, legi_no in enumerate(results): sheet.write (row, 0, legi_no) book.save("C:\\legi results.xls") 

它find并拿起结果,如果我打印variables'cc'。 但是写入Excel电子表格是不成功的,因为它只写入第一个单元格。

任何帮助将不胜感激。 谢谢。

您可以for bb in aa循环中for bb in aa创buildresultsvariables。

这意味着results将被初始化为[]中的每个值,最后结果将只包含一个元素(最后一个),这当然不是目的。

results放在外面,应该可以正常工作,如下所示。

 import mechanize import re import xlwt from bs4 import BeautifulSoup import urllib2 book = xlwt.Workbook(encoding='utf-8', style_compression = 0) sheet = book.add_sheet('Legi', cell_overwrite_ok = True) for items in ['university student', 'high school student']: url = r'http://legistar.council.nyc.gov/Legislation.aspx' request = mechanize.Request(url) response = mechanize.urlopen(request) forms = mechanize.ParseResponse(response, backwards_compat=False) form = forms[0] response.close() form['ctl00$ContentPlaceHolder1$txtSearch'] = items submit_page = mechanize.urlopen(form.click()) soup = BeautifulSoup(submit_page.read()) aa = soup.find_all(href=re.compile('LegislationDetail')) results = [] # Initialize results here !!! for bb in aa: cc = bb.text #print cc results.append(cc) for row, legi_no in enumerate(results): sheet.write (row, 0, legi_no) book.save("C:\\legi results.xls")