python - 使用python从没有数据名称的json文件中提取url

标签 python json data-extraction information-extraction objectpath

我有 json 文件,其中包含 900 篇文章的元数据,我想从中提取 URL。我的文件是这样开始的

[
{
    "title": "The histologic phenotypes of …",
    "authors": [
        {
            "name": "JE Armes"
        },
    ],
    "publisher": "Wiley Online Library",
    "article_url": "https://onlinelibrary.wiley.com/doi/abs/10.1002/(SICI)1097-0142(19981201)83:11%3C2335::AID-CNCR13%3E3.0.CO;2-N",
    "cites": 261,
    "use": true
},

{
    "title": "Comparative epidemiology of pemphigus in ...",
    "authors": [
        {
            "name": "S Bastuji-Garin"
        },
        {
            "name": "R Souissi"
        }
        ],
        "year": 1995,
        "publisher": "search.ebscohost.com",
    "article_url": "http://search.ebscohost.com/login.aspx?direct=true&profile=ehost&scope=site&authtype=crawler&jrnl=0022202X&AN=12612836&h=B9CC58JNdE8SYy4M4RyVS%2FrPdlkoZF%2FM5hifWcv%2FwFvGxUCbEaBxwQghRKlK2vLtwY2WrNNl%2B3z%2BiQawA%2BocoA%3D%3D&crl=c",
    "use": true
    },
 .........

我想使用 objectpath 检查文件以创建 json.tree 以提取 URL。这是我要执行的代码

  1.    import json
  2.    import objectpath
  3.    with open("Data_sample.json") as datafile: data = json.load(datafile)
  4.    jsonnn_tree = objectpath.Tree(data['name of data'])
  5.    result_tuple = tuple(jsonnn_tree.execute('$..article_url'))

但是在创建树的步骤 4 中,我必须插入我认为文件中没有的数据名称。我怎样才能替换这条线?

最佳答案

您可以使用列表理解获取所有文章网址。

import json

with open("Data_sample.json") as fh:
    articles = json.load(fh)

article_urls = [article['article_url'] for article in articles]

关于python - 使用python从没有数据名称的json文件中提取url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796366/

相关文章:

python - Django 中的完整性错误

java - 调用 WS REST 时为 "No content to map to Object due to end of input"

c# - 解析复杂 json 字符串的最佳实践

xml - 用户评论的数据提取

r - 如何加快从栅格中提取缓冲区中土地覆盖类型的比例?

python - numpy 中余弦的真正反函数? (不是 arccos)

python - 使用 mailgun (python) 发送带有附件的电子邮件时出错

Power BI 中的 Python Plotly

c# - DataContractJsonSerializer 和 JsonConvert 给出不同的结果

python - Python 中的复杂数据提取