How do I download a file over HTTP using Python?

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

    In python3 you can use urllib3 and shutil libraires. Download them by using pip or pip3 (Depending whether python3 is default or not)

    pip3 install urllib3 shutil
    

    Then run this code

    import urllib.request
    import shutil
    
    url = "http://www.somewebsite.com/something.pdf"
    output_file = "save_this_name.pdf"
    with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
        shutil.copyfileobj(response, out_file)
    

    Note that you download urllib3 but use urllib in code

提交回复
热议问题