我是Python的新手,我一直在浏览本网站上的Q& A,以回答我的问题。但是,我是初学者,我发现很难理解一些解决方案。我需要一个非常基本的解决方案。
有人可以向我解释“通过http下载文件”和“将其保存到Windows中的磁盘”的简单解决方案吗?
我也不确定如何使用shutil和os模块。
我要下载的文件不到500 MB,是一个.gz存档文件。如果有人可以解释如何提取存档并利用其中的文件,那就太棒了!
这是一个部分解决方案,我从各种答案中总结出来:
import requests
import os
import shutil
global dump
def download_file():
global dump
url = "http://randomsite.com/file.gz"
file = requests.get(url, stream=True)
dump = file.raw
def save_file():
global dump
location = os.path.abspath("D:\folder\file.gz")
with open("file.gz", 'wb') as location:
shutil.copyfileobj(dump, location)
del dump
有人可以指出错误(初学者级别)并解释任何更简单的方法吗?
谢谢!
答案 0 :(得分:188)
下载文件的简洁方法是:
import urllib
testfile = urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz", "file.gz")
从网站下载文件并将其命名为file.gz
。这是我最喜欢的解决方案之一,来自Downloading a picture via urllib and python。
此示例使用urllib
库,它将直接从源文件中检索文件。
答案 1 :(得分:101)
如上所述here:
import urllib
urllib.urlretrieve ("http://randomsite.com/file.gz", "file.gz")
EDIT:
如果您仍想使用请求,请查看this question或this one。
答案 2 :(得分:32)
我使用wget。
简单而好的图书馆,如果你想要举例?
import wget
file_url = 'http://johndoe.com/download.zip'
file_name = wget.download(file_url)
wget模块支持python 2和python 3版本
答案 3 :(得分:25)
使用wget,urllib和request的四种方法。
#!/usr/bin/python
import requests
from StringIO import StringIO
from PIL import Image
import profile as profile
import urllib
import wget
url = 'https://tinypng.com/images/social/website.jpg'
def testRequest():
image_name = 'test1.jpg'
r = requests.get(url, stream=True)
with open(image_name, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
def testRequest2():
image_name = 'test2.jpg'
r = requests.get(url)
i = Image.open(StringIO(r.content))
i.save(image_name)
def testUrllib():
image_name = 'test3.jpg'
testfile = urllib.URLopener()
testfile.retrieve(url, image_name)
def testwget():
image_name = 'test4.jpg'
wget.download(url, image_name)
if __name__ == '__main__':
profile.run('testRequest()')
profile.run('testRequest2()')
profile.run('testUrllib()')
profile.run('testwget()')
testRequest - 4469882函数调用(4469842原始调用)在20.236秒内
testRequest2 - 8580函数调用(8574个原始调用),0.072秒
testUrllib - 3836函数调用(3775个原始调用),0.036秒
testwget - 在0.020秒内进行3489次函数调用
答案 4 :(得分:3)
异乎寻常的Windows解决方案
import subprocess
subprocess.run("powershell Invoke-WebRequest {} -OutFile {}".format(your_url, filename), shell=True)
答案 5 :(得分:2)
import urllib
urllib.request.urlretrieve("https://raw.githubusercontent.com/dnishimoto/python-deep-learning/master/list%20iterators%20and%20generators.ipynb", "test.ipynb")
将单个原始 juypter 笔记本下载到文件中。
答案 6 :(得分:0)
我开始沿着这条路走下去,因为ESXi的wget没有使用SSL编译,我想从供应商的网站直接将OVA下载到世界另一端的ESXi主机上。
我必须通过编辑规则(正确)来禁用防火墙(懒惰)/启用https
创建了python脚本:
import ssl
import shutil
import tempfile
import urllib.request
context = ssl._create_unverified_context()
dlurl='https://somesite/path/whatever'
with urllib.request.urlopen(durl, context=context) as response:
with open("file.ova", 'wb') as tmp_file:
shutil.copyfileobj(response, tmp_file)
ESXi库有点配对,但开源weasel安装程序似乎使用了urllib for https ...所以它激发了我沿着这条路走下去
答案 7 :(得分:0)
对于 Python3 + ,URLopener
已过时。
使用时会出现如下错误:
url_opener = urllib.URLopener()AttributeError:模块'urllib'没有 属性“ URLopener”
所以,尝试:
import urllib.request
urllib.request.urlretrieve(url, filename)
答案 8 :(得分:-1)
对于文本文件,您可以使用:
import requests
url = 'https://WEBSITE.com'
req = requests.get(url)
path = "C:\\YOUR\\FILE.html"
with open(path, 'wb') as f:
f.write(req.content)
答案 9 :(得分:-5)
另一种保存文件的简洁方法是:
import csv
import urllib
urllib.retrieve("your url goes here" , "output.csv")