首页 > Linux操作系统 > Linux操作系统 > Python下载文件实例


原创 Linux操作系统 作者:jieforest 时间:2012-06-12 09:24:45 0 删除 编辑
Downloading files from the internet is something that almost every programmer will have to do at some point.

Python provides several ways to do just that in its standard library. Probably the most popular way to download a file is over HTTP using the urllib or urllib2 module.

Python also comes with ftplib for FTP downloads.

Finally there’s a new 3rd party module that’s getting a lot of buzz called requests. We’ll be focusing on the two urllib modules and requests for this article.

Since this is a pretty simple task, we’ll just show a quick and dirty script. that downloads the same file with each library and names the result slightly differently. We will download a zipped file from this very blog for our example script. Let’s take a look:


import urllib
import urllib2
import requests

url = ''

print "downloading with urllib"
urllib.urlretrieve(url, "")

print "downloading with urllib2"
f = urllib2.urlopen(url)
data =
with open("", "wb") as code:

print "downloading with requests"
r = requests.get(url)
with open("", "wb") as code:

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

下一篇: PyPy 1.9发布
请登录后发表评论 登录


  • 博文量
  • 访问量