2016-12-06 53 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]是令我困惑的部分,因爲它列表中的對象太多... –