2016-07-01 10 views
1

私はPythonでモジュールtkinterを使用してGUIベースのプロジェクトを作成しています。それは、美しいスープを使用して、オンラインジャッジから基本データ、例えばSPOJを取り出す。私はPythonの初心者なので、私が書いたことのほとんどはインターネットからの基本的なチュートリアルです。しかし、コードの特定の部分については、私は完全に立ち往生しています。私はこの部分をテストした後のpython submissions.pyIndexError:BeautifulSoupを使用して関数の範囲外のインデックスをリストします。

でそれを実行したときに

import sys 
import urllib.request 
from bs4 import BeautifulSoup 
import re 

userName = 'xilinx' 
spojUrl = 'http://www.spoj.com/users/'+userName 

with urllib.request.urlopen(spojUrl) as x: 
    html = x.read() 
soup = BeautifulSoup(html, 'html.parser') 
# li - list of successful submissions 
li = soup.find_all('table', class_='table table-condensed')[0].find_all('td') 
listOfSolvedProblemCodes = [] 
    for submission in li: 
     problemCode = submission.get_text() 
     if problemCode: 
      listOfSolvedProblemCodes.append(problemCode) 
print (userName+ ' has solved',len(listOfSolvedProblemCodes),'problems on Spoj.') 

コードのこの部分は正常に動作し、私は、問題が発生し、より大きなコードにそれを取り入れてみてください。

def show(ch, userName): 

    if ch == 'SPOJ': 

     spojUrl = 'http://www.spoj.com/users/'+userName 

     with urllib.request.urlopen(spojUrl) as x: 
      html = x.read() 
     soup = BeautifulSoup(html, 'html.parser') 
     li = soup.find_all('table', class_='table table-condensed')[0].find_all('td') 
     listOfSolvedProblemCodes = [] 
     for submission in li: 
      problemCode = submission.get_text() 
      if problemCode: 
       listOfSolvedProblemCodes.append(problemCode) 


    # then collect more information from soup and print it through labels in another window 

    det = tkinter.Tk() 
    det.title("Statistics") 
    det.geometry("800x600") 

しかしはIndexErrorの問題はラインでstats.pyで発生します。

stats.pyで
def compStats(): 
    if ch == "SPOJ": 
     stats.show(ch, userName) 

B2 = tkinter.Button(root, text="My Statistics", command=compStats) 
B2.place(anchor = W, x = 30, y = 220, width=200) 

:frame.pyで

:私はここで、コードの該当部分を含めています:

li = soup.find_all('table', class_='table table-condensed')[0].find_all('td') 

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\Aa\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init .py", line 1550, in __call

return self.func(*args)

File "frame.py", line 34, in compStats

stats.show(ch, userName)

File "C:\Users\Aa\AppData\Local\Programs\Python\Python35-32\stats.py", line 17, in show

li = soup.find_all('table', class_='table table-condensed')[0].find_all('td')

IndexError: list index out of range

コードがここで動作しない理由を理解できません。助けてください!

答えて

3

これをデバッグする際の最初の手順は、エラーを投げている複雑な行を取り出して簡単にすることです。中間値を検査して、コードについての前提が真であるかどうかを確認することができます。この場合、実際にはsoup.find_all('table', ...)が何かを見つけているという前提があります。例えば

、この変更:あなたはtablesがあることがわかります

print("tables is", tables) 

:これまで

li = soup.find_all('table', class_='table table-condensed')[0].find_all('td') 

tables = soup.find_all('table', class_='table table-condensed') 
li = tables[0].find_all('td') 

次に、tmpを調べるためにprint文を追加します空である可能性が高いので、あなたがしようとするとtables[0]インデックス0が範囲外です。

+0

私はあなたが提案したアプローチを試してみましたが、同じ問題が見つかってテーブルが空であることがわかりました。しかし、htmlファイルから、私はテーブルが空であってはならないことを知っています。このコードを別々に実行すると、同じことが確認されます。答えは3138、つまり解決された問題の数、この場合はtdタグの数です。したがって、混乱。同じコードが異なる動作をするのはなぜですか? – Neha

+0

同じ理由で、私は別々に実行して答えを得る完全なコードを入れました。他の人もそれを実行して確認できるようにします。しかし、いったん関数に入れると、インデックス外のエラーが表示されます。 – Neha

+0

@ネハ:あなたが異なる出力を得ているならば、おそらく異なる入力を与えているでしょう。同じ変数名だけでなく変数の実際の内容をすべてのケースで同じ入力としているという前提を確認しましたか?多分 'username'はあなたが思うものではないでしょう。 –

関連する問題