salamanderMH
V2EX  ›  问与答

Python requests 库下载文件有点问题

  •  
  •   salamanderMH · Jan 27, 2021 · 686 views
    This topic created in 1937 days ago, the information mentioned may be changed or developed.

    简单代码:

    def save_file(self, folder, url):
        if not url:
            return
    
        filename = url.rsplit("/", 1)[-1]
        path = os.path.join(folder, filename)
        if os.path.exists(path):
            return path
    
        r = requests.get(url)
        with open(path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
        return path
    

    这个方法下载文件有时候会有问题,会下载不完整,python 也不是特别熟,看不出啥问题呢,请大佬们指教。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4153 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    ♥ Do have faith in what you're doing.