【问题标题】:Open multiple Json files with URL's and download the files contained in each using Python使用 URL 打开多个 Json 文件并使用 Python 下载每个文件中包含的文件
【发布时间】:2021-11-17 05:01:13
【问题描述】:

我们将在一个单独的目录中接收多达 10k 个 JSON 文件,这些文件必须被解析并转换为单独的 .csv 文件。然后必须将每个 URL 中的文件下载到另一个目录。我计划在 Mac 上的 Automator 中执行此操作并调用 Python 脚本来下载文件。我已经完成了 shell 脚本的一部分以转换为 CSV,但不知道从哪里开始使用 python 来下载 URL。

这是迄今为止我对 Automator 的了解:

    - Shell = /bin/bash
    - Pass input = as arguments
    - Code = as follows


#!/bin/bash

/usr/bin/perl -CSDA -w <<'EOF' - "$@" > ~/Desktop/out_"$(date '+%F_%H%M%S')".csv
use strict;
use JSON::Syck;
$JSON::Syck::ImplicitUnicode = 1;

# json node paths to extract
 my @paths = ('/upload_date', '/title', '/webpage_url');

for (@ARGV) {
    my $json;
    open(IN, "<", $_) or die "$!";
    {
        local $/; 
        $json = <IN>;
    }
    close IN;
    my $data = JSON::Syck::Load($json) or next;
    my @values = map { &json_node_at_path($data, $_) } @paths;
    {
        #   output CSV spec
        #   - field separator = SPACE
        #   - record separator = LF
        #   - every field is quoted
        local $, = qq( );
        local $\ = qq(\n);
        print map { s/"/""/og; q(").$_.q("); } @values;
    }
}

sub json_node_at_path ($$) {
    #   $ : (reference) json object
    #   $ : (string) node path
    # 
    #   E.g. Given node path = '/abc/0/def', it returns either
    #       $obj->{'abc'}->[0]->{'def'}   if $obj->{'abc'} is ARRAY; or
    #       $obj->{'abc'}->{'0'}->{'def'} if $obj->{'abc'} is HASH.
    my ($obj, $path) = @_;  
    my $r = $obj;
    for ( map { /(^.+$)/ } split /\//, $path ) {
        if ( /^[0-9]+$/ && ref($r) eq 'ARRAY' ) {
        $r = $r->[$_];
        }
        else {
             $r = $r->{$_};
        }
    }
    return $r;
}
EOF

【问题讨论】:

  • 如果你想从 URL 下载文件,你应该考虑在 python 中使用requests 模块
  • 你收到了反对票,因为 A. 你没有尝试自己先解决问题,B. 没有产生 minimum reproducible example,C. 已经有多个 questions 提出相同的问题事物。此问题表明您没有尝试解决问题,而是依靠他人为您完成工作。

标签: python python-3.x automator


【解决方案1】:

我不熟悉 Automator,所以也许其他人可以解决这个问题,但就 Python 部分而言,从 url 下载文件相当简单。它会是这样的:

import requests

r = requests.get(url) # assuming you don't need to do any authentication
with open("my_file_name", "wb") as f:
    f.write(r.content)

Requests 是一个很好的处理 http(s) 的库,并且由于 Response 的内容属性是一个字节字符串,我们可以打开一个用于写入字节的文件(“wb”)并直接写入它。这也适用于可执行有效负载,因此请确保您知道您正在下载什么。如果您尚未安装请求,请运行 pip install requests 或 Mac 等效项。

如果您倾向于在 python 中完成整个过程,我建议您查看 jsoncsv 包。这两者都是标准库的一部分,并为您正在做的事情提供高级接口

编辑: 如果您在这样的文件上使用 json 模块,下面是一个示例:

[
  {
  "url": <some url>,
  "name": <the name of the file>
  }
]

您的 Python 代码可能如下所示:

import requests
import json

with open("my_json_file.json", "r") as json_f:
    for item in json.load(json_f)
        r = requests.get(item["url"])
        with open(item["name"], "wb") as f:
            f.write(r.content)

【讨论】:

  • 感谢您的帮助!我需要在我的代码中明确定义 JSON 文件的名称还是 *.json 可以正常工作?
  • 为此您需要查看osos.listdir 可能有用
猜你喜欢
  • 2021-09-19
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2014-01-15
  • 2023-03-28
  • 2019-12-24
  • 1970-01-01
相关资源
最近更新 更多