Python download a file

后端 未结 2 1924
借酒劲吻你
借酒劲吻你 2021-01-25 07:17

I\'m unsure how to do this. One way is:

import urllib.request;
urllib.request.urlretrieve(\'www.example.com/file.tar\', \'file.tar\')

Another w

2条回答
  •  自闭症患者
    2021-01-25 07:38

    As @jdi said, you may use the requests library. This is also mentioned on https://docs.python.org/2/library/urllib2.html. You will need to pip the library, e.g.

    pip install requests
    

    My code looks like this:

    import requests
    
    def download_file(url):
        file = requests.get(url)
        return file.text
    

    It can't be easier.

提交回复
热议问题