Python 将结果写入.xls(向网页提交2个查询,并将不同的结果存储到.xls)
大家好…我正在使用Python 2.76将查询提交到.aspx网页,并通过BeautifulSoup获取结果,并希望将其存储到Excel电子表格中Python 将结果写入.xls(向网页提交2个查询,并将不同的结果存储到.xls),python,excel,beautifulsoup,xlwt,Python,Excel,Beautifulsoup,Xlwt,大家好…我正在使用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'
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")
如果我打印变量“cc”,它会查找并拾取结果。但是,写入Excel电子表格并不成功,因为它只写入第一个单元格
任何帮助都将不胜感激。谢谢。您在aa循环中为bb创建
结果变量
这意味着results
将被初始化为[]
,用于aa
中的每个值,最终结果将只包含一个元素(最后一个),这当然不是预期的
将结果
放在外部,它应该可以正常工作,如下所示
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")