下载 Python 中的互联网资源并将其保存在我想要的位置 [英] Download an internet resource in Python and save it on my desired location

查看:26
本文介绍了下载 Python 中的互联网资源并将其保存在我想要的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我正在使用 urllib2 通过互联网下载文件.我正在使用此代码

导入 urllib2response = urllib2.urlopen('http://www.example.com/myfile.zip')...

这段代码实际上是将 zip 文件保存在我的临时文件夹中,我不想那样,我想将它保存在我想要的位置.可能吗?

解决方案

您可以使用 urllib.urlretrieve 函数,将远程文件下载到本地文件系统.

<预><代码>>>>导入 urllib>>>urllib.urlretrieve('http://www.example.com/myfile.zip', 'path/to/download/dir/myfile.zip')

请参阅 urllib.urlretrieve 文档 了解更多信息.

I am new to Python and I am using urllib2 to download files over the internet. I am using this code

import urllib2
response = urllib2.urlopen('http://www.example.com/myfile.zip')
...

This code actually save the zip file on my temp folder, I don't want it to be like that, I want to save it on my desired location. Is it possible?

解决方案

You can use the urllib.urlretrieve function, to download the distant file to your local filesystem.

>>> import urllib
>>> urllib.urlretrieve('http://www.example.com/myfile.zip', 'path/to/download/dir/myfile.zip')

See the urllib.urlretrieve documentation for more info.

这篇关于下载 Python 中的互联网资源并将其保存在我想要的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆