简体   繁体   中英

extract urls from json file without data name using python

i have json file that containd the metadata of 900 articles and i want to extract the Urls from it. my file start like this

[
{
    "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
    },
 .........

I want to inspect the file with objectpath to create json.tree for the extraxtion of the url. this is the code i want to execute

  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'))

But in the step 4 for the creation of the tree, I have to insert the name of the data whitch i think that i haven't in my file. How can i replace this line?

You can get all the article urls using a list comprehension.

import json

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

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

You can instantiate the tree like this:

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

And in the end:

results = [x for x in results]

will yield:

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

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

jsonnn_tree = objectpath.Tree(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM