Selenium webdriver在循环中占用所有内存

时间:2014-08-11 22:27:16

标签: python linux selenium-webdriver debian phantomjs

我遇到进程和内存使用问题。我创建了一个脚本,使用selenium webdriver和PhantomJS解析一些网页。该脚本运行正常,但我的代码需要一些优化。

我启动脚本,过了一会儿,我的记忆已经满了,因为我有很多phantomjs进程。

如何解决此问题?我的代码:

server.py

from selenium import webdriver
import radio
import urllib2
class Server(object):
    running = False
    radio_dir = "db/radio.txt"
    def __init__(self):
        """Choose browser from selenium webdrivers"""
        self.browser2 = webdriver.PhantomJS()
        self.r = radio.RadioParser(self.browser2)
        self.running = True
    def loop(self):
        """main server loop"""
        while self.running:
            radio_str = self.r.parse()
            self.save(self.radio_dir, radiozet_str)
            time.sleep(30)
    def save(self, location, string):
        """Put location and string to override db files"""
        try:
            file = open(location, 'w')
            file.write(string)
        except IOError:
            print "IOError"
        finally:
            file.close()

if __name__ == "__main__":
    s = Server()
    s.loop()

和radio.py

class RadioParser(object):
    url = "http://www.radiourl/index.html"
    def __init__(self, browser):
        self.driver = browser
    def parse(self):
        self.driver.get(self.url)
        song = (self.driver.find_element_by_class_name("artist").text +u" - " +
                self.driver.find_element_by_class_name("album").text)
        print song
        return str(song)

1 个答案:

答案 0 :(得分:1)

您应该使用

关闭您的webdriver
self.driver.quit()