【问题标题】:extract urls from json file without data name using python使用python从没有数据名的json文件中提取url
【发布时间】:2018-12-15 19:05:59
【问题描述】:

我有一个包含 900 篇文章元数据的 json 文件,我想从中提取 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 步中,我必须插入我认为我的文件中没有的数据名称。我该如何替换这条线?

【问题讨论】:

  • 如果对您有帮助,请投票。

标签: python json data-extraction information-extraction objectpath


【解决方案1】:

您可以使用列表推导式获取所有文章网址。

import json

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

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

【讨论】:

    【解决方案2】:

    你可以像这样实例化树:

    tobj = op.Tree(your_data)
    results = tobj.execute("$.article_url")
    

    最后:

    results = [x for x in results]
    

    将产生:

    ["url1", "url2", ...]
    

    【讨论】:

      【解决方案3】:

      您是否尝试删除引用并仅使用:

      jsonnn_tree = objectpath.Tree(data)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        相关资源
        最近更新 更多