【问题标题】:how to retrieve json files from url inside a text file如何从文本文件中的url检索json文件
【发布时间】:2021-10-16 23:02:13
【问题描述】:

我必须使用 curl 检查来自 url 的 json 数据 curl -H "User-agent: 'your bot 0.1'" url.json | jq 此代码正在运行

我想试试这个包含 200 个 url 的 .txt 文件 像这些

https://www.reddit.com/user/wanderer_007_.json

https://www.reddit.com/....

https://www.reddit.com/....

https://www.reddit.com/....

https://www.reddit.com/....

这些只是示例。但是每当我将文本文件作为输入时

#!/usr/bin/bash

while read -r line; do
    name="$line"
   curl -H "User-agent: 'your bot 0.1'" $name | jq 
done < test001.txt


curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL

但如果我尝试单独使用该网址,它会按预期工作

【问题讨论】:

  • 应该是 $name 而不是 url.json?我复制了代码并没有收到错误
  • 我只使用了 $name ,但它对我不起作用,我现在已经编辑了问题
  • 我仍然没有得到错误。您将收到来自jq 的错误,因为reddit.com 没有返回json 响应(parse error: Invalid numeric literal at line 1, column 10)。如果文本文件中的 url 是将接收 json 响应的请求,您的代码应该可以工作。好像你发的网址(reddit.com)不是真实的,你能发下真实的吗?
  • 网址来自 reddit,只有它们像 reddit.com/user/wanderer_007_.json 这将获得我需要的 json 数据
  • 我使用您的代码和该类型的 url 获得了所需的输出,您确定您的 test001.txt 文件包含您认为它包含的内容吗?

标签: json linux bash curl web-scraping


【解决方案1】:

尝试用双引号将文件名包含在 curl 命令中:

curl -H "User-agent: 'your bot 0.1'" "$name" | jq

该错误可能是由于 Bash 专门解释了 URL 中的一些奇怪字符。例如,URL 中的空格可能会导致 $name 拆分为 2 个命令行参数,从而使 curl 无法解析其输入。

【讨论】:

  • 我也试过了,但我不知道我的虚拟盒子到底发生了什么
  • 谢谢你解决了,问题出在我使用的设备上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
相关资源
最近更新 更多