How do I download a file over HTTP using Python?

前端 未结 25 3422
感动是毒
感动是毒 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:45

    Just for the sake of completeness, it is also possible to call any program for retrieving files using the subprocess package. Programs dedicated to retrieving files are more powerful than Python functions like urlretrieve. For example, wget can download directories recursively (-R), can deal with FTP, redirects, HTTP proxies, can avoid re-downloading existing files (-nc), and aria2 can do multi-connection downloads which can potentially speed up your downloads.

    import subprocess
    subprocess.check_output(['wget', '-O', 'example_output_file.html', 'https://example.com'])
    

    In Jupyter Notebook, one can also call programs directly with the ! syntax:

    !wget -O example_output_file.html https://example.com
    

提交回复
热议问题