2012-04-19 4 views
1

Excel 시트의 온라인 웹 페이지에서 HTML 표의 내용을 넣으려고합니다 (작은 파이썬 스크립트 사용).BeautifulSoup + xlwt : 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) 
- 그래서 아무것도 엑셀 시트에 가고,하지만 난 VAR의 내용을 인쇄 할 때 ... 내가 볼 수 무엇을 볼!

나는 한 시간이 지나면 그것을 고치려고 노력하고 있지만, 아직 어떤 일이 벌어지는 지 이해하지 못합니다. 당신 중 일부는 내게 손을 줄 수 있다면, 그것은 아주 좋은되어야합니다.

+0

실제로 문제가 무엇인지 이해할 수 없습니다. "엑셀 시트에 어떤 것이 든 들어갈 수 있지만, 내가 var의 내용을 인쇄 할 때 ... 나는 무엇을보아야 하는가?"문장을 확장하십시오. – Nemoverflow

+1

죄송합니다! 나는 콧노래 (td.text와 같음)가 Excel 시트에 넣고 싶은 값을 반환한다는 것을 의미합니다 (인쇄 할 때 Excel 시트에 쓰고 싶은 것을 볼 수 있습니다). –

+0

ws.write (x, y, td.text)가 실제로이 오류를 반환합니다. ValueError : 열 인덱스 (257)의 범위가 아닌 정수 (256) –

답변

0

신청서를 작성했습니다. 그리고 나는 td.text의 출력이 excel 파일과 동일하다는 것을 확신합니다. 그럼 네 질문은 뭐니? 원하는 내용이 아닌 경우 BeautifulSoap의 사용법을 확인해야합니다. 더 자세히 알려면 다음을 수행해야 할 수 있습니다.

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

도움 주셔서 감사합니다! 그것은 지금 일하는 것 ... 무슨 일이 있었는지 모르겠지만 ...하지만 난 항상 첫 번째 국가 후에이 오류가 발생 : 예외 : 시도 셀 : sheetname = u'BULATS_IA 'rowx = 1 colx = 0 –

관련 문제