当我打电话给班级时,为什么我的for-loop会终止?

时间:2012-01-14 17:46:54

标签: python

每次程序在第三次运行时都会终止:

for component_ID in self.component_IDs:
    print component_ID
    fundamentals(component_ID)

这是我的fundamentals课程:

class fundamentals(download):

    def __init__(self, ID):
        self.browser = spynner.Browser()
        self.url = self.getURL(ID)
        self.browser.load(self.url)
        html = self.browser.html
        print "1"
        soup = BeautifulSoup(html)
        print "2"
        self.count = self.get_count(soup)
        self.currency = self.get_currency(soup)
        self.fundamentals = self.get_fundamentals_pages(soup)
        self.write_fundamentals_to_db(id, self.currency, self.count, 
                                      self.fundamentals)

当汤=美丽(汤)第3次执行时,程序总是停止。 这不取决于提供的component_ID

component_ID的长度为30,当我在索引2处开始循环时,它也会运行两次并在第3次运行时停止。

这种奇怪的行为是如何发生的,如何在下次阻止它?

更多信息:
此外,程序在第3次打印1后才会在没有错误消息的情况下终止。我不知道现在该做什么。

当我只实现.close()但是如上所述调用循环时,它会在经过循环一次后停止。当我将实例保存在属性self.component_ID中时,循环在循环两次后中断。

编辑:我通过关闭spynner.browser并更改循环的工作方式解决了此问题的上一个版本。我不是仅仅调用类,而是构建一个列表并将实例附加到它上面。

1 个答案:

答案 0 :(得分:0)

关闭spynner.browser并更改循环的工作方式。而不仅仅是调用类,构建一个列表并将实例附加到它。