Scr*_*oob 6 python firefox selenium selenium-chromedriver selenium-webdriver
所以我一直在研究10k +页面上的刮刀并从中抓取数据.
问题是随着时间的推移,内存消耗急剧增加.因此,为了克服这一点而不是仅在scrape scraper结束时关闭驱动程序实例,因此它会在每个页面加载并提取数据后关闭实例.
但由于某种原因,ram记忆仍然存在.
我尝试使用PhantomJS但由于某种原因它没有正确加载数据.我还尝试使用初始版本的scraper将Firefox中的缓存限制为100mb,这也不起作用.
注意:我使用chromedriver和firefox实例运行测试,不幸的是我不能使用诸如请求,机械化等库来代替selenium.
任何帮助都表示赞赏,因为我一直试图解决这个问题一周.谢谢.
强制 Python 解释器向操作系统释放内存的唯一方法是终止进程。因此,用于multiprocessing生成 selenium Firefox 实例;当生成的进程终止时,内存将被释放:
import multiprocessing as mp
import selenium.webdriver as webdriver
def worker()
driver = webdriver.Firefox()
# do memory-intensive work
# closing and quitting is not what ultimately frees the memory, but it
# is good to close the WebDriver session gracefully anyway.
driver.close()
driver.quit()
if __name__ == '__main__':
p = mp.Process(target=worker)
# run `worker` in a subprocess
p.start()
# make the main process wait for `worker` to end
p.join()
# all memory used by the subprocess will be freed to the OS
Run Code Online (Sandbox Code Playgroud)
您是否想说您的驱动程序正在填充您的记忆?你如何关闭它们?如果您正在提取数据,您是否仍然引用某些将它们存储在内存中的集合?
您提到,当您在抓取结束时关闭驱动程序实例时,内存已经耗尽,这使得您看起来像是保留了额外的引用。