【问题标题】:Extracting URLS from json file从 json 文件中提取 URL
【发布时间】:2020-05-08 15:56:01
【问题描述】:

我使用邮递员从 api 获取网址,以便查看某些标题。 响应保存为 .json 文件。

我的 response.json 文件的 sn-p 如下所示:

{
    "apiUrl":"https://api.ft.com/example/83example74-3c9b-11ea-a01a-example547046735",
    "title": {
        "title": "Example title example title example title"
    },
    "lifecycle": {
        "initialPublishDateTime":"2020-01-21T22:54:57Z",
        "lastPublishDateTime":"2020-01-21T23:38:19Z"
    },
    "location":{
        "uri":"https://www.ft.com/exampleurl/83example74-3c9b-11ea-a01a-example547046735"
    },
    "summary": "...",
    # ............(this continues for all different titles I found)
}

由于我想查看文章,因此我想生成所有 url 的列表。我对 apiUrl 不感兴趣,而只对 uri 感兴趣。

我当前的python文件是这样的

with open ("My path to file/response.json") as file:
    for line in file:
        urls = re.findall('https://(?:[-\www.]|(?:%[\da-fA-F]{2}))+', line)
        print(urls)

这给了我以下输出: ['https://api.ft.com', 'https://www.ft.com', 'https://api.ft.com', 'https://www.ft.com',........

但是,我希望能够看到 www.ft.com 的整个网址(所以不是 api.ft.com 网址,因为我对这些不感兴趣)。 例如,我希望我的程序提取如下内容:https://www.ft.com/thisisanexampleurl/83example74-3c9b-11ea-a01a-example547046735

我希望程序对整个响应文件执行此操作

有人知道怎么做吗?

所有帮助将不胜感激。 雷蒙德

【问题讨论】:

  • 您能否提供更好的数据 sn-p?如果 JSON 格式正确,使用 json.load 而不是正则表达式可能会好得多。
  • 这是一个一级深度的 JSON 吗?所需的 URL 是否始终存储在 uri 键下?
  • 想要的 url 总是跟在 uri 键之后

标签: python json url postman extract


【解决方案1】:

有很多方法可以从下面提取是最简单的表示吧

str_='first url "https://api.ft.com/example/83example74-3c9b-11ea-a01a-example547046735" plus second url "https://www.ft.com/exampleurl/83example74-3c9b-11ea-a01a-example547046735'
import re
re.findall("(?P<url>https?://[^\s]+)", str_)
Output=
['https://api.ft.com/example/83example74-3c9b-11ea-a01a-example547046735"', 'https://www.ft.com/exampleurl/83example74-3c9b-11ea-a01a-example547046735']

【讨论】:

  • 亲爱的。感谢您的遮阳篷,但我认为它没有帮助。如果我使用 ''''' with open ("/My Path to file/response.json") 作为文件:对于文件中的字符串:urls = ("(?Phttps?://[^\s ]+)", string) print(urls)''''''' 生成的输出是我的整个文件,不幸的是不仅是想要的 url
  • @Raymondvanzonneveld 检查这些
  • 查看输出我认为这不是我需要的。我只想要第二个 url (ft.com/..........) 而不是 api url (api.ft.com/......)
【解决方案2】:

假设 url 分散在 json 对象中,您可以递归搜索每个键处的每个嵌套对象值以确定它是否为 url。

此外,如果这是格式正确的 json,则使用 json.loads 将比文件对象更容易搜索。

例如使用pythonvalidators

import validators

Iterate through the object.

Check each value with -> `validators.url(value)`

If True -> return value

【讨论】:

  • 亲爱的@blakeyoder,感谢您的回复,这可能正是我所需要的。你能详细说明一下步骤吗?我似乎卡住了。我通过执行'validators.url('www.ft.com')导入了验证器并进行了检查。但是,当我执行“If True -> return www.ft.com”时,我得到一个无效的语法错误。
  • 您遇到什么语法错误?你能粘贴你的完整sn-p吗?
  • 我尝试了以下方法: ''''''import validators with open('/Users/raymondvanzonneveld/Desktop/API Folder/response.json', 'r' ) as myfile: data=myfile .read() for line in data: validators.url('www.ft.com') if True: print('www.ft.com')'''''' 我摆脱了语法错误,但现在我只是得到大量 www.ft.com 作为回报,所以不是完整的网址
  • 您将不正确的值传递给validators.url 您想要传递 json 键的值而不是字符串文字。我建议不要将其作为文件读取,而是使用json.loads 将其作为解析的 json 加载。为此,网络上有很多教程可以帮助将 json 字符串转换为已解析的对象 pythonbasics.org/read-json-file :)
【解决方案3】:

如果您确定哪些键包含 URL,您可以使用 nested_lookup 库来检索它们:

from nested_lookup import nested_lookup

urls = []
for key in ('uri', 'apiUrl'):
    urls.extend(nested_lookup(key, data))
print(urls)

# ['https://www.ft.com/exampleurl/83example74-3c9b-11ea-a01a-example547046735', 'https://api.ft.com/example/83example74-3c9b-11ea-a01a-example547046735']

【讨论】:

    【解决方案4】:

    感谢大家的意见。

    我找到了另一种方法来解决我的问题(我使用了 python 的 newsapi。基本上做了同样的事情,但是只查看金融时间 api 我现在得到了更多的网站和文章)。这对我来说效果更好

    雷蒙德·范·宗内维尔德

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多