1

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

[
{
    "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 步中,我必须插入我认为我的文件中没有的数据名称。我怎样才能替换这条线?

4

3 回答 3

0

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

jsonnn_tree = objectpath.Tree(data)
于 2018-12-15T19:24:32.110 回答
0

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

import json

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

article_urls = [article['article_url'] for article in articles]
于 2018-12-15T19:12:27.267 回答
0

您可以像这样实例化树:

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

最后:

results = [x for x in results]

将产生:

["url1", "url2", ...]
于 2018-12-15T19:15:44.250 回答