2016-12-06 57 views
-1

我试图使用Scopus API检索由scopus分配给作者的唯一作者标识号。在某些情况下,名称会产生多个作者ID。我的问题是在阅读JSON格式,这样我可以解析出“DC:标识符”将字典解析为许多值并在python中创建表格

这里是我的代码:

import requests 
    import json 

    url = "http://api.elsevier.com/content/search/author" 

    querystring = {"apiKey":"xxxxxxxxxxxxx","query":["AUTHLASTNAME(Volaire)","AUTHFIRST(F)"]} 

    headers = { 
    'cache-control': "no-cache", 
    'postman-token': "5c0df5ad-2645-8e88-0f8a-10795757b4ec" 
    } 

    response = requests.request("GET", url, headers=headers, params=querystring) 

    results = response.json() 

然后我得到以下输出:

{ 
    "search-results": { 
    "opensearch:totalResults": "3", 
    "opensearch:startIndex": "0", 
    "opensearch:itemsPerPage": "3", 
    "opensearch:Query": { 
     "@role": "request", 
     "@searchTerms": "AUTHLASTNAME(Volaire)", 
     "@startPage": "0" 
    }, 
    "link": [ 
     { 
     "@_fa": "true", 
     "@href": "http://api.elsevier.com/content/search/author?start=0&count=25&query=AUTHLASTNAME%28Volaire%29&apiKey=4275854a6db51fb373ef69fb1337ccf9", 
     "@ref": "self", 
     "@type": "application/json" 
     }, 
     { 
     "@_fa": "true", 
     "@href": "http://api.elsevier.com/content/search/author?start=0&count=25&query=AUTHLASTNAME%28Volaire%29&apiKey=4275854a6db51fb373ef69fb1337ccf9", 
     "@ref": "first", 
     "@type": "application/json" 
     } 
    ], 
    "entry": [ 
     { 
     "@_fa": "true", 
     "link": [ 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/author/author_id/55910750900", 
      "@ref": "self" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/search/author?query=au-id%2855910750900%29", 
      "@ref": "search" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=55910750900&origin=inward", 
      "@ref": "scopus-citedby" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=55910750900&origin=inward", 
      "@ref": "scopus-author" 
      } 
     ], 
     "prism:url": "http://api.elsevier.com/content/author/author_id/55910750900", 
     "dc:identifier": "AUTHOR_ID:55910750900", 
     "eid": "9-s2.0-55910750900", 
     "preferred-name": { 
      "surname": "Volaire", 
      "given-name": "Florence A.", 
      "initials": "F.A." 
     }, 
     "name-variant": [ 
      { 
      "@_fa": "true", 
      "surname": "Volaire", 
      "given-name": "F.", 
      "initials": "F." 
      }, 
      { 
      "@_fa": "true", 
      "surname": "Volaire", 
      "given-name": "Florence", 
      "initials": "F." 
      } 
     ], 
     "document-count": "41", 
     "subject-area": [ 
      { 
      "@abbrev": "ENVI", 
      "@frequency": "7", 
      "$": "Environmental Science (all)" 
      }, 
      { 
      "@abbrev": "AGRI", 
      "@frequency": "50", 
      "$": "Agricultural and Biological Sciences (all)" 
      }, 
      { 
      "@abbrev": "ENGI", 
      "@frequency": "4", 
      "$": "Engineering (all)" 
      } 
     ], 
     "affiliation-current": { 
      "affiliation-url": "http://api.elsevier.com/content/affiliation/affiliation_id/60020315", 
      "affiliation-id": "60020315", 
      "affiliation-name": "INRA Institut National de La Recherche Agronomique", 
      "affiliation-city": "Paris", 
      "affiliation-country": "France" 
     } 
     }, 
     { 
     "@_fa": "true", 
     "link": [ 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/author/author_id/6603570012", 
      "@ref": "self" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/search/author?query=au-id%286603570012%29", 
      "@ref": "search" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=6603570012&origin=inward", 
      "@ref": "scopus-citedby" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=6603570012&origin=inward", 
      "@ref": "scopus-author" 
      } 
     ], 
     "prism:url": "http://api.elsevier.com/content/author/author_id/6603570012", 
     "dc:identifier": "AUTHOR_ID:6603570012", 
     "eid": "9-s2.0-6603570012", 
     "preferred-name": { 
      "surname": "Volaire", 
      "given-name": "Florence", 
      "initials": "F." 
     }, 
     "name-variant": [ 
      { 
      "@_fa": "true", 
      "surname": "Volaire", 
      "given-name": "F.", 
      "initials": "F." 
      } 
     ], 
     "document-count": "8", 
     "subject-area": [ 
      { 
      "@abbrev": "AGRI", 
      "@frequency": "13", 
      "$": "Agricultural and Biological Sciences (all)" 
      }, 
      { 
      "@abbrev": "BIOC", 
      "@frequency": "1", 
      "$": "Biochemistry, Genetics and Molecular Biology (all)" 
      } 
     ], 
     "affiliation-current": { 
      "affiliation-url": "http://api.elsevier.com/content/affiliation/affiliation_id/60020315", 
      "affiliation-id": "60020315", 
      "affiliation-name": "INRA Institut National de La Recherche Agronomique", 
      "affiliation-city": "Paris", 
      "affiliation-country": "France" 
     } 
     }, 
     { 
     "@_fa": "true", 
     "link": [ 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/author/author_id/24571987100", 
      "@ref": "self" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "http://api.elsevier.com/content/search/author?query=au-id%2824571987100%29", 
      "@ref": "search" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=24571987100&origin=inward", 
      "@ref": "scopus-citedby" 
      }, 
      { 
      "@_fa": "true", 
      "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=24571987100&origin=inward", 
      "@ref": "scopus-author" 
      } 
     ], 
     "prism:url": "http://api.elsevier.com/content/author/author_id/24571987100", 
     "dc:identifier": "AUTHOR_ID:24571987100", 
     "eid": "9-s2.0-24571987100", 
     "preferred-name": { 
      "surname": "Volaire", 
      "given-name": "M.", 
      "initials": "M." 
     }, 
     "name-variant": [ 
      { 
      "@_fa": "true", 
      "surname": "Volaire", 
      "given-name": null, 
      "initials": "M." 
      } 
     ], 
     "document-count": "1", 
     "subject-area": [ 
      { 
      "@abbrev": "CHEM", 
      "@frequency": "2", 
      "$": "Chemistry (all)" 
      }, 
      { 
      "@abbrev": "BIOC", 
      "@frequency": "1", 
      "$": "Biochemistry, Genetics and Molecular Biology (all)" 
      }, 
      { 
      "@abbrev": "ENVI", 
      "@frequency": "1", 
      "$": "Environmental Science (all)" 
      } 
  1. 我需要做的是解析这个,以便得到'dc:标识符'被拉起。在这种情况下有3

  2. 我非常希望把这个变成一个表,其中我有 AUTHLASTNAME/AUTHFIRST/DCIDENTIFER(列)

赞赏任何意见。

+0

发布的代码与问题无关(关于处理它生成的数据);它是否正确? –

+0

@ScottHunter:这是正确的,只是它告诉你我是如何到达json输出的。 –

+0

你如何得到这些数据是不相关的;并且您发布的输出不完整。 –

回答

0

这将让该信息为第1部分:

print([e["dc:identifier"] for e in results['search-results']['entry']]) 

您可以使用每个entry其他信息类似的东西。

至于第2部分:python没有表格。

0

也许这样? 你在这里有什么是轨道:)

print(results['search-results']['link']['entry']['dc:identifier']) 

试试这个。它应该工作,因为你想要的值是在你的JSON列表中的对象内。

some_dict = [ 
    { 
     "dc:identifiers": r['dc:identifier'], 
     "AUTHLASTNAME": r['preferred-name']['surname'], 
     "AUTHFIRST": r['preferred-name']['given-name'], 
    } for r in results['search-results']['entry'] 
] 

很明显,假设你的json保持这种格式。 还检查我把结果['search-results'] ['entry']实际上是正确的。我没有三倍检查

+0

谢谢,但我得到这个:“SyntaxError:解析时意外的EOF” –

+0

我猜[search-results] [entry]是令我困惑的部分,因为它列表中的对象太多... –