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

时间:2022-10-30 19:24:33

I have problem with processes and memory usage. I created a script to parse some webpages using selenium webdriver with PhantomJS. The script works fine, but my code need some optimization.

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

I start script and after a moment, my memory is full because I have a lot of phantomjs processes.

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

How to fix this? my code:

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

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()

and 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 个解决方案

#1


1  

You should close your webdriver, using

你应该关闭你的webdriver,使用

self.driver.quit()    

#1


1  

You should close your webdriver, using

你应该关闭你的webdriver,使用

self.driver.quit()