更新时间:2022-11-17 10:47:24
您是否尝试过使用请求库?我相信它提供了一个对urllib的抽象。
Have you tried using the requests library? I believe it provides an abstraction over urllib.
下面的解决方案应该为你工作,但它使用请求库而不是urllib(但请求> urllib反正!让我知道您是否喜欢继续使用urllib。
The following solution should work for you, but it uses the requests library instead of urllib (but requests > urllib anyway!). Let me know if you prefer to continue using urllib.
import os
import requests
def download(url, chunk_s=1024, fname=None):
if not fname:
fname = url.split('/')[-1]
req = requests.get(url, stream=True)
with open(fname, 'wb') as fh:
for chunk in req.iter_content(chunk_size=chunk_s):
if chunk:
fh.write(chunk)
return os.path.join(os.getcwd(), fname)