Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;索引器:列表索引超出范围“;带elif语句_Python - Fatal编程技术网

Python &引用;索引器:列表索引超出范围“;带elif语句

Python &引用;索引器:列表索引超出范围“;带elif语句,python,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

我已经编写了一个脚本,它解析来自某些网站的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
导入urllib
级别城市:
定义初始化(自我、城市名称、链接):
self.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(自我):
filehandle=urllib.urlopen(self.url)
#将结果中的行获取到数组中
lines=filehandle.readlines()
#(对于每个)在每行中循环通过每行
行号=0#行号计数器
对于行中的行:
行号=行号+1增量计数器
#查找字符串,否则位置为-1
位置1=行.rfind('#f2')
如果位置1>0:
self.high0=行[行号]。拆分('&')[0]。拆分('>')[1]#下一行:高
self.low0=行[line_number+10]。拆分('&')[0]。拆分('>')[1]#下一行:低
elif位置1<0:
位置1=line.rfind(“>隔夜”)
如果位置1>0:
self.high0=行[line_number+9]。拆分(“&”)[0]。拆分(“:”)[1]#下一行:高
self.low0=行[line_number+15]。拆分('&')[0]。拆分(':')[1]#下一行:低
当position1=line.rfind('#f2')时,脚本工作正常。但是,当它找不到“#f2”(它只位于第一个站点的html代码中,而不是第二个站点),我会尝试告诉它查找“>过夜”,然后提取“:”和“&”之间的数据。“数据”将始终是单个数字。我想一个问题可能是,我试图提取的数字两边都有一个空格,但我不确定如何解决这个问题。当我运行脚本时,会出现以下错误:

<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>
self.high0=行[line_number+9]。拆分(“&”)[0]。拆分(“:”)[1]#下一行:高 “索引器:列表索引超出范围”

以下是我正在为第一个网站解析的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>
  • 3度;C
从第二个网站(我收到错误的网站):

隔夜

有可能下雨

低:3度;C

周四

有可能下雨

高:9度;C


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

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

  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
def retrievetemp(self):
filehandle=urllib.urlopen(self.url)
lines=filehandle.read()#已修改
位置1=行.rfind('#f2')
如果位置1>0:
self.high0=行[position1:].split('&')[0]。split('>')[1]#下一行:high
self.low0=行[position1+10:].split('&')[0]。split('>')[1]#下一行:low
elif位置1<0:
位置1=lines.rfind(“>过夜”)
如果位置1>0:
self.high0=行[position1+9:].split('&')[0]。split(':')[1]#下一行:高
self.low0=行[position1+15:].split('&')[0]。split(':')[1]#下一行:low

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

请正确格式化源代码,看看它解析HTML非常简单快速,没有必要重新发明轮子……我是这个网站的新手,我认为我正确地格式化了源代码?你不使用xml解析器有什么原因吗?这使得解析html比手工编写搜索代码容易一百万倍。我以前从未使用过这种方法,并且已经编写过类似的代码,所以我只是做了一些更改。不过,如果这证明太难,我会尝试一下。谢谢你的回复。一旦我做了更改,就会出现以下错误:AttributeError:'list'对象没有属性'split',ok,lines=??在你的程序中。我想这是你粘贴的html输出对不起,我想我应该在我原来的帖子中包含所有的代码。我已经更新了源代码。这有帮助吗?@RahulGautam!它输出两个站点的数量,唯一的问题是它没有倒计时我指定的行数。它只是在第一行加上一个“&”和“>”,并且每次都使用这个数字。@GrijeshChauhan谢谢你的提示,我下次会这样做。