文件(.tar.gz)下载处理使用urlib和requests package-python

幻影

SCOPE:使用哪个库?urllib Vs 请求 我试图下载一个 url 上可用的日志文件。URL 托管在 aws 上并包含文件名。访问该 url 后,它会提供一个 .tar.gz 文件供下载。我需要在我选择的目录中下载这个文件 untar 并解压缩它以到达其中的 json 文件,最后解析 json 文件。在网上搜索时,我发现零星的信息散布在各处。在这个问题中,我尝试将它合并到一个地方。

幻影

使用REQUESTS库:一个 PyPi 包,在处理高 http 请求时被认为是优越的。参考资料:

  1. https://docs.python.org/3/library/urllib.request.html#module-urllib.request
  2. urllib、urllib2、urllib3 和 requests 模块之间有什么区别?

代码:

import requests
import urllib.request
import tempfile
import shutil
import tarfile
import json
import os
import re

with requests.get(respurl,stream = True) as File:
    # stream = true is required by the iter_content below
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        with open(tmp_file.name,'wb') as fd:
            for chunk in File.iter_content(chunk_size=128):
                fd.write(chunk)

with tarfile.open(tmp_file.name,"r:gz") as tf:
    # To save the extracted file in directory of choice with same name as downloaded file.
    tf.extractall(path)
    # for loop for parsing json inside tar.gz file.
    for tarinfo_member in tf:
        print("tarfilename", tarinfo_member.name, "is", tarinfo_member.size, "bytes in size and is", end="")
        if tarinfo_member.isreg():
            print(" a regular file.")
        elif tarinfo_member.isdir():
            print(" a directory.")
        else:
            print(" something else.")
        if os.path.splitext(tarinfo_member.name)[1] == ".json":
            print("json file name:",os.path.splitext(tarinfo_member.name)[0])
            json_file = tf.extractfile(tarinfo_member)
            # capturing json file to read its contents and further processing.
            content = json_file.read()
            json_file_data = json.loads(content)
            print("Status Code",json_file_data[0]['status_code'])
            print("Response Body",json_file_data[0]['response'])
            # Had to decode content again as it was double encoded.
            print("Errors:",json.loads(json_file_data[0]['response'])['errors'])


将提取的文件保存在与下载文件同名的选择目录中。变量“path”的构成如下。

其中 url 示例包含文件名“ 44301621eb-response.tar.gz

https://yoursite.com/44301621eb-response.tar.gz?AccessKeyId=your_id&Expires=1575526260&Signature=you_signature

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_NAME = 'your_project_name'
PROJECT_ROOT = os.path.join(BASE_DIR, PROJECT_NAME)
LOG_ROOT = os.path.join(PROJECT_ROOT, 'log')
filename = re.split("([^?]+)(?:.+/)([^#?]+)(\?.*)?", respurl)
# respurl is the url from the where the file will be downloaded 
path = os.path.join(LOG_ROOT,filename[2])

regex101.com 的正则表达式匹配输出 在此处输入图片说明

与 urllib 的比较

为了了解细微的差异,我也使用 urllib 实现了相同的代码。

请注意,临时文件库的用法略有不同,这对我有用。由于我们使用 urllib 和请求获得的响应对象不同,我不得不使用带有 urllib 的shutil 库,其中请求不适用于shutil 库copyfileobj 方法。

with urllib.request.urlopen(respurl) as File:
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        shutil.copyfileobj(File, tmp_file)

with tarfile.open(tmp_file.name,"r:gz") as tf:
    print("Temp tf File:", tf.name)
    tf.extractall(path)
    for tarinfo in tf:
        print("tarfilename", tarinfo.name, "is", tarinfo.size, "bytes in size and is", end="")
        if tarinfo.isreg():
            print(" a regular file.")
        elif tarinfo.isdir():
            print(" a directory.")
        else:
            print(" something else.")
        if os.path.splitext(tarinfo_member.name)[1] == ".json":
            print("json file name:",os.path.splitext(tarinfo_member.name)[0])
            json_file = tf.extractfile(tarinfo_member)
            # capturing json file to read its contents and further processing.
            content = json_file.read()
            json_file_data = json.loads(content)
            print("Status Code",json_file_data[0]['status_code'])
            print("Response Body",json_file_data[0]['response'])
            # Had to decode content again as it was double encoded.
            print("Errors:",json.loads(json_file_data[0]['response'])['errors'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用python ftp tar文件

在Python中读取.tar.gz文件

使用python通过SSH通过tar通过Tar传输文件

如何使用 gnupg python 模块解密 tar.gz 文件

使用Python提取tar文件的最快方法

如何使用 Python 在 flat tar.gz 中保存 tar 文件

Grafana tar 文件下载

使用httpClient和Angular下载.tar文件

Python3使用tar文件中的csv文件

Python 3:从tar.gz存档中提取文件

从python中的tar存档中提取压缩的gz文件

使用pip安装下载的tar.gz文件

使用 python 从 .tar.gz 文件中仅提取 jpg 文件

使用 Ansible 下载 hadoop-3.2.2.tar.gz 文件后校验和错误

与MacOS tar相比,为什么使用Python的tar库时tar.xz文件小15倍?

使用 Boto3 和 Python 将 tar.gz 文件上传到 S3 存储桶

如何使用Python创建完整的压缩tar文件?

使用PHP创建.tar.gz文件

使用 python setup.py install 在 dist 文件夹中创建 tar.gz

如何不使用pip install从tar.gz文件安装Python软件包

使用格式为 tar.gz.part* 的 python tarfile 提取所有部分文件

使用python将目录压缩到内存中的tar.gz文件中

Python -- 仅在目标文件夹中查找特定模式的 tar.gz 文件

在python中,如果解压缩tar.gz文件,如何获取或设置结果文件的名称

如何在 Python 中的 zip 文件中提取 tar.gz 文件的成员

Python:从tar.gz中提取具有模式的特定文件,而无需提取完整文件

提取.gz文件中的.TAR文件

sftp命令获取/下载.tar.gz文件

如何从下载的 tar.gz 文件安装 R Studio?