基本的http文件下载并保存到python中的磁盘



我一直在浏览这个网站上的问答,以回答我的问题。但是,我是初学者,我发现很难理解某些解决方案。我需要一个非常基本的解决方案。

有人可以向我解释"通过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:folderfile.gz")
    with open("file.gz", 'wb') as location:
        shutil.copyfileobj(dump, location)
    del dump

有人可以指出错误(初学者级别)并解释任何更简单的方法吗?

下载文件的一种简洁方法是:

import urllib
testfile = urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz", "file.gz")

这会从网站下载一个文件并将其命名为 file.gz 。这是我最喜欢的解决方案之一,从 通过urllib和python下载图片。

此示例使用 urllib 库,它将直接从源中检索文件。

对于 Python3+ URLopener 已被弃用。使用时,会出现如下错误:

url_opener = 网址。URLopener() AttributeError: module 'urllib' 没有 属性"URLopener"

因此,请尝试:

import urllib.request 
urllib.request.urlretrieve(url, filename)

如下所述:

import urllib
urllib.urlretrieve ("http://randomsite.com/file.gz", "file.gz")

EDIT: 如果您仍然想使用请求,请查看这个问题或这个问题。

使用 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 - 在 20.236 秒内4469882函数调用(4469842 个基元调用)

testRequest2 - 0.072 秒内完成 8580 次函数调用(8574 次基元调用)

testUrllib - 0.036 秒内完成 3810 次函数调用(3775 次原语调用)

testwget - 0.020 秒内调用 3489 次函数

我使用wget。

简单而好的库,如果你想举例?

import wget
file_url = 'http://johndoe.com/download.zip'
file_name = wget.download(file_url)

WGET 模块支持 Python 2 和 Python 3 版本

异国情调的 Windows 解决方案

import subprocess
subprocess.run("powershell Invoke-WebRequest {} -OutFile {}".format(your_url, filename), shell=True)
import urllib.request
urllib.request.urlretrieve("https://raw.githubusercontent.com/dnishimoto/python-deep-learning/master/list%20iterators%20and%20generators.ipynb", "test.ipynb")

将单个原始 Juypter 笔记本下载到文件。

对于文本文件,您可以使用:

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)

我之所以走这条路,是因为 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 库有点配对,但开源鼬鼠安装程序似乎使用 urllib 进行 https...所以它激励我走这条路

保存文件的另一种干净方法是:

import csv
import urllib
urllib.retrieve("your url goes here" , "output.csv")

最新更新