Python 3.5 中 urllib.urlretrieve 的替代
Alternative of urllib.urlretrieve in Python 3.5
我目前正在 UDACITY 学习机器学习课程。他们在那里用 python 2.7 编写了一些代码,但由于我目前使用的是 python 3.5,所以出现了一些错误。这是代码
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
我试过了urllib.request。
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
但还是报错。
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
我正在使用 PyCharm 作为我的 IDE。
您会使用 urllib.request.urlretrieve
。请注意此函数 "may become deprecated at some point in the future",因此您最好使用不太可能被弃用的接口:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
对于足够小的文件,您可以只 read
和 write
整个事情并完全放弃循环。
我知道这个问题早就有人回答了,但我会为未来的观众做出贡献。
建议的解决方案很好,但主要问题是如果您使用无效的 url,它会生成空文件。
作为此问题的解决方法,我调整了代码:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
希望对您有所帮助。
您可以使用 shutil.copyfileobj()
神奇地从 url 字节流复制到文件。
import urllib.request
import shutil
url = "http://www.somewebsite.com/something.pdf"
output_file = "save_this_name.pdf"
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
来源:
我目前正在 UDACITY 学习机器学习课程。他们在那里用 python 2.7 编写了一些代码,但由于我目前使用的是 python 3.5,所以出现了一些错误。这是代码
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
我试过了urllib.request。
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
但还是报错。
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
我正在使用 PyCharm 作为我的 IDE。
您会使用 urllib.request.urlretrieve
。请注意此函数 "may become deprecated at some point in the future",因此您最好使用不太可能被弃用的接口:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
对于足够小的文件,您可以只 read
和 write
整个事情并完全放弃循环。
我知道这个问题早就有人回答了,但我会为未来的观众做出贡献。
建议的解决方案很好,但主要问题是如果您使用无效的 url,它会生成空文件。
作为此问题的解决方法,我调整了代码:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
希望对您有所帮助。
您可以使用 shutil.copyfileobj()
神奇地从 url 字节流复制到文件。
import urllib.request
import shutil
url = "http://www.somewebsite.com/something.pdf"
output_file = "save_this_name.pdf"
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
来源: