search

Home  >  Q&A  >  body text

Python怎么下载URL指向的文件?

请问大家Python怎样下载一个url请求的文件,可能我描述的不是很清楚,直接贴代码。

比如是这样的url:http://www.aaa.com/download.php?aid=123

在浏览器中打开这个url,就会直接下载一个文档,请问如何用Python下载这个url指向的文档?

PHP中文网PHP中文网2888 days ago554

reply all(5)I'll reply

  • PHPz

    PHPz2017-04-17 17:32:50

    Just write to the file
    f=urllib2.urlopen(url)
    data=f.read()
    f=open(path,'wb')
    f.write(data)
    f.close()

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:32:50

    urllib2.urlretrieve(url, filename)

    So lazy, just google it →_→

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:32:50

    There is a problem with the above answer. There is no urlretrieve function in urllib2. This function is in urllib.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:32:50

    The simplest implementation is to use wget

    1. Installation

       pip install wget
      
    2. Use

      import wget
      wget.download("http://xxx.xxx.com/xx.mp4")

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:32:50

    Directly use the requests library to download. There are many examples on the Internet. There are also corresponding download methods for large files.

    reply
    0
  • Cancelreply