Python high memory usage with BeautifulSoup

后端 未结 4 1165
后悔当初
后悔当初 2020-12-19 03:24

I was trying to process several web pages with BeautifulSoup4 in python 2.7.3 but after every parse the memory usage goes up and up.

This simplified code produces th

4条回答
  •  佛祖请我去吃肉
    2020-12-19 04:03

    Try garbage collecting:

    from bs4 import BeautifulSoup
    import gc
    
    def parse():
        f = open("index.html", "r")
        page = BeautifulSoup(f.read(), "lxml")
        page = None
        gc.collect()
        f.close()
    
    while True:
        parse()
        raw_input()
    

    See also:

    Python garbage collection

提交回复
热议问题