Py学习  »  Python

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

rachid rachid • 4 年前 • 355 次点击  

我有一个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 为url的extraction创建json.tree。这是我要执行的代码

  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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49704
 
355 次点击  
文章 [ 3 ]  |  最新文章 4 年前
bspeagle
Reply   •   1 楼
bspeagle    5 年前

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

jsonnn_tree = objectpath.Tree(data)
andreihondrari
Reply   •   2 楼
andreihondrari    5 年前

可以这样实例化树:

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

最后:

results = [x for x in results]

将产生:

["url1", "url2", ...]
Bryan
Reply   •   3 楼
Bryan    5 年前

您可以使用列表理解获得所有文章的url。

import json

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

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