python - "IndexError: list index out of range"与 elif 语句

标签 python

我编写了一个脚本,可以解析某些网站的 html 代码以提取特定数据。我从两个不同的站点提取这些数据,因此我使用 elif 语句。这是代码:

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 > 0 :
                            self.high0 = lines[line_number].split('&')[0].split('>')[1] # next line: high
                            self.low0 = lines[line_number + 10].split('&')[0].split('>')[1] # next line:low
                    elif position1 < 0 :
                            position1 = line.rfind('>Overnight')
                            if position1 > 0 :
                                    self.high0 = lines[line_number + 9].split('&')[0].split(':')[1] # next line: high
                                    self.low0 = lines[line_number + 15].split('&')[0].split(':')[1] # next line:low

当position1 = line.rfind('#f2')时,脚本完美运行。但是,当它找不到“#f2”时(这仅位于第一个站点的 html 代码中,而不是第二个站点的 html 代码中),我试图告诉它查找“>Overnight”,然后提取之间的数据':' 和 '&'。 “数据”始终是一个数字。我认为一个问题可能是我试图提取的数字两侧都有空格,但我不确定如何解决这个问题。当我运行脚本时出现错误:

self.high0 =lines[line_number + 9].split('&')[0].split(':')[1] # 下一行:高 “IndexError:列表索引超出范围”

作为引用,这里是我为第一个网站解析的 html 代码:

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

以及第二个网站(我收到错误的网站):

<p class="txt-ctr-caps">Overnight<br><br></p>
            <p><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%" /></p>
        <p>Rain<br>Likely<br></p>
            <p class="point-forecast-icons-low">Low: 3 &deg;C</p>
    </div>
    <div class="one-ninth-first">
            <p class="txt-ctr-caps">Thursday<br><br></p>
            <p><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%" /></p>
        <p>Rain<br>Likely<br></p>
            <p class="point-forecast-icons-high">High: 9 &deg;C</p>
    </div>

任何帮助将不胜感激,谢谢!!

最佳答案

现在您已经提供了完整的代码: 试试这个

  def retrieveTemps(self) :
        filehandle = urllib.urlopen(self.url)
        lines = filehandle.read() # modified

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

即使这样也无助于尝试使用基本的打印语句来调试代码,例如处理时变量中的行和行。

关于python - "IndexError: list index out of range"与 elif 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12720392/

相关文章:

python - 在终端中激活不同的 Python 版本

python - 如何在 python 中找到格式中两个时间之间的差异?

python - 如何使用 PuLP 的 Gurobi 求解器设置 MIP 启动(初始解决方案)?

python - 使用pyculib进行3D FFT结果错误

python - Pandas 计算连续行之间存在 X 秒差异的次数

python - 将 RTSP 流转发到远程套接字(RTSP 代理?)

python - 无法在 Google App Engine (Python) 中显示图像

python - pandas 多索引级别内按列排序

python - Kivy BoxLayout - 将小部件移动到顶部

python - 计算素数的所有可能因数