How do I download a file over HTTP using Python?

前端 未结 25 3467
感动是毒
感动是毒 2020-11-21 07:17

I have a small utility that I use to download an MP3 file from a website on a schedule and then builds/updates a podcast XML file which I\'ve added to iTunes.

The te

25条回答
  •  半阙折子戏
    2020-11-21 07:46

    import os,requests
    def download(url):
        get_response = requests.get(url,stream=True)
        file_name  = url.split("/")[-1]
        with open(file_name, 'wb') as f:
            for chunk in get_response.iter_content(chunk_size=1024):
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)
    
    
    download("https://example.com/example.jpg")
    

提交回复
热议问题