BeautifulSoup + xlwt:将HTML表格的内容放入Excel中

我正在尝试(用一个小python脚本)将一个在线网页的HTML表格的内容放在Excel工作表中。

一切工作都很好,除了“Excel的东西”。

#!/usr/bin/python # --*-- coding:UTF-8 --*-- import xlwt from urllib2 import urlopen import sys import re from bs4 import BeautifulSoup as soup import urllib def BULATS_IA(name_excel): """ Function for fetching the BULATS AGENTS GLOBAL LIST""" ws = wb.add_sheet("BULATS_IA") # I add a sheet in my excel file Countries_List = ['United Kingdom','Albania','Andorra'] Longueur = len(Countries_List) number = 1 print("Starting to fetch ...") for Countries in Countries_List: x = 0 y = 0 print("Fectching country %s on %s" % (number, Longueur)) number = number + 1 htmlSource = urllib.urlopen("http://www.cambridgeesol.org/institutions/results.php?region=%s&type=&BULATS=on" % (Countries)).read() s = soup(htmlSource) **tableauGood = s.findAll('table') try: rows = tableauGood[3].findAll('tr') for tr in rows: cols = tr.findAll('td') y = 0 x = x + 1 for td in cols: hum = td.text ws.write(x,y,td.text) y = y + 1 wb.save("%s.xls" % name_excel)** except (IndexError): pass print("Finished for IA") name_doc_out = raw_input("What do you want for name for the Excel output document ? >>> ") wb = xlwt.Workbook(encoding='utf-8') print("Starting with BULATS Agents, then with BULATS IA") #BULATS_AGENTS(name_doc_out) BULATS_IA(name_doc_out) 

– 那么Excel表格里面什么都有,但是当我打印var的内容的时候…我看到了我应该看到的东西!

我试图从一个小时以来修复它,但我仍然不明白发生了什么事情。 如果你们中的一些人可以帮我一把,那应该非常好。

我已经尝试你的应用程序。 我很确定td.text的输出和excel文件是一样的。 那么你的问题是什么? 如果内容不是你想要的,你应该检查BeautifulSoap的用法。 此外,您可能需要执行以下操作:

  for td in cols: hum = td.text.replace(" ", " ") print hum ws.write(x,y,hum)