简体   繁体   English

OSError:[Errno 24]打开的文件太多

[英]OSError: [Errno 24] Too many open files

I'm using python and selenium to scrape a website full of text files (as URL's), and then using requests to get those txt files. 我正在使用python和selenium来抓取一个充满文本文件(作为URL)的网站,然后使用请求来获取这些txt文件。

The code I'm using is as follows: 我使用的代码如下:

r = requests.get(link,cookies=cookies)

    # Checking for a successful connection to the server.
    if r.status_code == 200:
        print("Downloading data for time %d, altitude %d" %(counter1, altitude) ) 
        data = r.text # Extracting the text from the file online
        file_name = os.path.join(path,fileName)  
        with open(file_name, 'w') as w:
            w.write(data)
        w.closed


    # Closing browser
    browser.close()

There are about 900 odd files to be downloaded, but after every 250 odd downloads/reqests, the script terminates with an error 大约有900个奇数文件要下载,但是每250次奇数次下载/请求后,脚本会因错误而终止

OSError. OSError。 [Errno 24] Too many files open. [错误24]打开的文件太多。

I've made sure the the file that was being written to is closed. 我确保正在写入的文件已关闭。 Same goes for selenium, after each text file is downloaded, the chromedriver closes, and the loop moves onto the next URL. 硒也是如此,下载完每个文本文件后,chromedriver关闭,循环移至下一个URL。 Has anyone else encountered this, if so, what did you do to fix it? 有没有其他人遇到过这个问题,如果是这样,您做了什么修复?

Thanks for suggestions. 感谢您的建议。

I just realized that browser.close() closes the window but does not quit the instance of chromedriver. 我只是意识到browser.close()关闭了窗口,但没有退出chromedriver实例。 Since the initilization of the chromedriver was within the loop of extracting the data file, the script kept opening new instances of chromedriver, eventually overloading my memory with over 200 instances. 由于chromedriver的初始化处于提取数据文件的循环之内,因此脚本不断打开chromedriver的新实例,最终使我的内存超过200个实例。

The simple fix to this is use webdriver.quit(), which will completely quit the instance of the webdriver. 最简单的解决方法是使用webdriver.quit(),它将完全退出Webdriver的实例。

Better still, instead of creating a new instance at the beginning of every loop iteration, at the end of the loop just use webdriver.get(URL), which will redirect the current instance to the target URL. 更好的是,不是在每个循环迭代的开始都创建一个新实例,而是在循环结束时使用webdriver.get(URL),它将当前实例重定向到目标URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSError: [Errno 24] 打开的文件太多 - OS Mojave - OSError: [Errno 24] Too many open files - OS Mojave OSError:[Errno 24]太多打开的文件python,ubuntu - OSError: [Errno 24] Too many open files python , ubuntu OSError: [Errno 24] 使用 Nibabel 打开的文件太多 - OSError: [Errno 24] Too many open files using Nibabel slackclient OSError:[Errno 24]打开的文件太多 - slackclient OSError: [Errno 24] Too many open files OSError: [Errno 24] 打开的文件太多; 在 python; 难以调试 - OSError: [Errno 24] Too many open files; in python; difficult to debug OSError: [Errno 24] 通过 Django admin 上传 9000+ csv 个文件时打开的文件太多 - OSError: [Errno 24] Too many open files when uploading 9000+ csv files through Django admin Django从一个图像域保存到另一个图像域:OSError:[Errno 24]打开的文件太多 - Django saving from one imagefield to another: OSError: [Errno 24] Too many open files 多处理“OSError:[Errno 24] 打开的文件太多”:如何清理作业和队列? - Multiprocessing "OSError: [Errno 24] Too many open files": How to clean up jobs and queues? OSError:[Errno 24]从终端调用脚本时打开的文件太多 - OSError: [Errno 24] Too many open files when invoking script from terminal OSError:[Errno 24]在Twisted中使用Reactor.run()时打开的文件过多 - OSError: [Errno 24] Too many open files when using reactor.run() in Twisted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM