urllib.request for python 3.3无法下载文件 [英] urllib.request for python 3.3 not working to download file
问题描述
我想使用python下载一个大型存档文件并保存,但是urllib不适用于我。这是我的代码:
I would like to download a large archive file with python and save it, but urllib is not working for me. This is my code:
import urllib
urllib.request("http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")
请注意,我在此使用的链接示例不是一个大的存档。我只是用它作为例子。它直接链接到.txt文件,所以它应该工作。我收到这个错误:
Note that the link I used in this example is not a large archive. I am only using it as an example. It links directly to a .txt file, so it should work. I am getting this error:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
urllib.request("http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")
AttributeError: 'module' object has no attribute 'request'
在我看来,像urllib一样破损,缺少请求方法。我使用的是Python 3.3。我应该使用另一个模块还是实际上是一个Python问题?
It seems to me like urllib is somehow broken and missing the "request" method. I am using Python 3.3. Should I be using another module or is it actually a Python problem?
推荐答案
不,它没有破坏。 urllib.request
文档很漂亮清楚这是如何工作的:
No, it is not broken. The urllib.request
documentation is pretty clear on how this works:
import urllib.request
req = urllib.request.urlopen('http://www.google.com')
data = req.read()
编辑:如果您需要将文件直接写入磁盘而不是处理数据,请使用 urlretrieve
。
If you need to write the file directly to disk rather than process the data, use urlretrieve
.
urllib.request.urlretrieve('http://example.com/big.zip', 'file/on/disk.zip')
这篇关于urllib.request for python 3.3无法下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!