1

オンライン Web ページの HTML テーブルの内容を Excel シートに入れようとしています (小さな Python スクリプトを使用)。

「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 の内容を印刷すると ... 表示されるはずのものが表示されます。

1時間から修正しようとしていますが、何が起こっているのかまだわかりません。あなたの何人かが私に手を差し伸べることができれば、それはとてもいいことです。

4

1 に答える 1

0

私はあなたのアプリケーションを試しました。そして、td.text の出力は Excel ファイルと同じであると確信しています。それで、あなたの質問は何ですか?内容がお望みのものでない場合は、BeautifulSoap の使用方法を確認してください。さらに、次のことを行う必要がある場合があります。

           for td in cols:
                hum =  td.text.replace(" ", " ")
                print hum
                ws.write(x,y,hum)
于 2012-04-19T06:38:08.350 に答える