尝试读取大型网站文件数据时出现MemoryError异常

时间:2018-04-29 18:50:11

标签: python memory beautifulsoup python-requests bigdata

我试图阅读大型网站数据,但即时面对这个MemoryError异常

import requests
requests.urllib3.disable_warnings()
search_page = "http://www.yachtworld.co.uk/core/listing/cache/searchResults.jsp?ps=99999"
y = requests.get(search_page, timeout=999999, stream=True)
result = y.text

当我尝试从MemoryError变量读取页面的输出时,我面临result例外

无论如何都要在不面对此异常的情况下阅读整个数据,

感谢。

1 个答案:

答案 0 :(得分:0)

据我所知,问题没有任何改变 - 这意味着没有可能,你可以加载数据,如同提交的here

我提供的链接中接受的答案说明了一个非常好的代码来分块响应:

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                #f.flush() commented by recommendation from J.F.Sebastian
    return local_filename