IndexError: list index out of range in a function using BeautifulSoup
<p>I am making a GUI based project in Python with the module tkinter. It fetches basic data from online judge, eg, SPOJ using Beautiful Soup. I am a newbie in Python, so most of the things I have written is with basic tutorials from internet. However, for a particular piece of code, I am totally stuck up.</p>

<pre><code>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.')
</code></pre>

<p>This part of code works fine when I run it with python submissions.py</p>

<p>After testing this part, I try to incorporate it into the larger code, where the problem arises. I am including here the relevant section of the code:</p>

<p>In frame.py:</p>

<pre><code>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)
</code></pre>

<p>In stats.py:</p>

<pre><code>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")
</code></pre>

<p>But the problem of IndexError occurs in <code>stats.py</code> in the line:</p>

<pre><code>li = soup.find_all('table', class_='table table-condensed')[0].find_all('td')
</code></pre>

<blockquote>
  <p>Exception in Tkinter callback</p>
  
  <p>Traceback (most recent call last):</p>
  
  <p>File
  "C:\Users\Aa\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init
  <strong>.py", line 1550, in __call</strong></p>
  
  <p>return self.func(*args)</p>
  
  <p>File "frame.py", line 34, in compStats</p>
  
  <p>stats.show(ch, userName)</p>
  
  <p>File "C:\Users\Aa\AppData\Local\Programs\Python\Python35-32\stats.py",
  line 17, in show</p>
  
  <p>li = soup.find_all('table', class_='table
  table-condensed')[0].find_all('td')</p>
  
  <p>IndexError: list index out of range</p>
</blockquote>

<p>I am unable to understand why the code is unable to work here. Please help!</p>