Python download a file

后端 未结 2 1926
借酒劲吻你
借酒劲吻你 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 08:03

    I am moving what started as comments, to an answer...

    Your second example is pretty much doing what it needs to, in making a request object with a custom header and then reading the results into a local file.

    urlretrieve is a higher level function so it only does exactly what the docs say: Downloads a network resource to a local file and tells you where the file is. If you don't like the slightly lower level approach of your second example and you want more higher-level functionality, you can look into using the Requests library

提交回复
热议问题