"IndexError: list index out of range" with elif statement
<p>I have written a script that parses html code from certain websites to pull specific data. I have two different sites that I pull this data from, so I use an elif statement. Here is the code: </p>

<pre><code>import urllib

class city :
    def __init__(self, city_name, link) :
            self.name = city_name
            self.url = link
            self.high0 = 0
            self.high1 = 0
            self.high2 = 0
            self.high3 = 0
            self.high4 = 0
            self.high5 = 0
            self.high6 = 0
            self.low0 = 0
            self.low1 = 0
            self.low2 = 0
            self.low3 = 0
            self.low4 = 0
            self.low5 = 0

    def retrieveTemps(self) :
            filehandle = urllib.urlopen(self.url)

            # get lines from result into array
            lines = filehandle.readlines()

            # (for each) loop through each line in lines
            line_number = 0 # a counter for line number
            for line in lines:
                    line_number = line_number + 1 # increment counter

# find string, position otherwise position is -1

                    position1 = line.rfind('#f2')
                    if position1 &gt; 0 :
                            self.high0 = lines[line_number].split('&amp;')[0].split('&gt;')[1] # next line: high
                            self.low0 = lines[line_number + 10].split('&amp;')[0].split('&gt;')[1] # next line:low
                    elif position1 &lt; 0 :
                            position1 = line.rfind('&gt;Overnight')
                            if position1 &gt; 0 :
                                    self.high0 = lines[line_number + 9].split('&amp;')[0].split(':')[1] # next line: high
                                    self.low0 = lines[line_number + 15].split('&amp;')[0].split(':')[1] # next line:low
</code></pre>

<p>The script works perfectly when position1 = line.rfind('#f2'). However, when it can't find '#f2' (this is only located in the html code of the first site, not the second), I am trying to tell it to look for '>Overnight' and then extract the data between the ':' and the '&amp;'. The 'data' will always be a single number. I am thinking that one problem may be there is a space on either side of this number I am trying to extract, but I am not sure how to resolve this issue. When I run the script I get the error: </p>

<p>self.high0 = lines[line_number + 9].split('&amp;')[0].split(':')[1] # next line: high
"IndexError: list index out of range"</p>

<p>For reference, here is the html code I am parsing for the first website: </p>

<pre><code>&lt;/h3&gt;&lt;img src="/weathericons/15.gif" longdesc="#f2" alt="Rain mixed with snow" title="Rain mixed with snow" /&gt;&lt;ul&gt;
              &lt;li class="high" title="High"&gt;3&amp;deg;C&lt;/li&gt;
              &lt;li class="low"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;li class="pop"&gt;&amp;nbsp;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/div&gt;
</code></pre>

<p>and from the second website (the one for which I am receiving the error): </p>

<pre><code>&lt;p class="txt-ctr-caps"&gt;Overnight&lt;br&gt;&lt;br&gt;&lt;/p&gt;
            &lt;p&gt;&lt;img src="/images/wtf/medium/nra60.png" width="86" height="86" alt="Rain Likely Chance for Measurable Precipitation 60%" title="Rain Likely Chance for Measurable Precipitation 60%" /&gt;&lt;/p&gt;
        &lt;p&gt;Rain&lt;br&gt;Likely&lt;br&gt;&lt;/p&gt;
            &lt;p class="point-forecast-icons-low"&gt;Low: 3 &amp;deg;C&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class="one-ninth-first"&gt;
            &lt;p class="txt-ctr-caps"&gt;Thursday&lt;br&gt;&lt;br&gt;&lt;/p&gt;
            &lt;p&gt;&lt;img src="/images/wtf/medium/ra70.png" width="86" height="86" alt="Rain Likely Chance for Measurable Precipitation 70%" title="Rain Likely Chance for Measurable Precipitation 70%" /&gt;&lt;/p&gt;
        &lt;p&gt;Rain&lt;br&gt;Likely&lt;br&gt;&lt;/p&gt;
            &lt;p class="point-forecast-icons-high"&gt;High: 9 &amp;deg;C&lt;/p&gt;
    &lt;/div&gt;
</code></pre>

<p>Any help would be much appreciated, thanks!!</p>
