原始代码取自此要点:rivaldi8/ImpactServlet.py但由于我使用的api密钥不适用于QUERY_URL
,因此我修改了以下代码:
class Scopus(ImpactService):
QUERY_URL = "https://api.elsevier.com/content/search/scopus?query=doi({1})&apiKey={0}&httpAccept=application/json"
API_KEY = "7f59af901d2d86f78a1fd60c1bf9426a"
def __init__(self, request):
self.doi = request.getParameter("doi")
def _getServiceResponse(self):
return requests.get(self.QUERY_URL.format(self.API_KEY, self.doi))
def _buildResponseToClient(self, serviceResponse):
serviceResponseObject = json.loads(serviceResponse.text)
citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
linkBack = self._getLinkBack(serviceResponseObject)
return self._getJsonDocument(citationCount, linkBack)
def _getLinkBack(self, scopusData):
linkList = scopusData["search-results"]["entry"]["0"]["link"]
linkBackObject = next(link for link in linkList if link["@ref"] == "scopus-citedby")
return linkBackObject["@href"]
下面的示例JSON数据可以从此URL检索:
{
"search-results": {
"opensearch:totalResults": "1",
"opensearch:startIndex": "0",
"opensearch:itemsPerPage": "1",
"opensearch:Query": {
"@role": "request",
"@searchTerms": "doi(10.1038/35016500)",
"@startPage": "0"
},
"link": [
{
"@_fa": "true",
"@ref": "self",
"@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=doi%2810.1038%2F35016500%29&apiKey=7f59af901d2d86f78a1fd60c1bf9426a&httpAccept=application/json",
"@type": "application/json"
},
{
"@_fa": "true",
"@ref": "first",
"@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=doi%2810.1038%2F35016500%29&apiKey=7f59af901d2d86f78a1fd60c1bf9426a&httpAccept=application/json",
"@type": "application/json"
}
],
"entry": [
{
"@_fa": "true",
"link": [
{
"@_fa": "true",
"@ref": "self",
"@href": "https://api.elsevier.com/content/abstract/scopus_id/0034729765"
},
{
"@_fa": "true",
"@ref": "author-affiliation",
"@href": "https://api.elsevier.com/content/abstract/scopus_id/0034729765?field=author,affiliation"
},
{
"@_fa": "true",
"@ref": "scopus",
"@href": "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
},
{
"@_fa": "true",
"@ref": "scopus-citedby",
"@href": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
}
],
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/0034729765",
"dc:identifier": "SCOPUS_ID:0034729765",
"eid": "2-s2.0-0034729765",
"dc:title": "Effect of aquaculture on world fish supplies",
"dc:creator": "Naylor R.",
"prism:publicationName": "Nature",
"prism:issn": "00280836",
"prism:volume": "405",
"prism:issueIdentifier": "6790",
"prism:pageRange": "1017-1024",
"prism:coverDate": "2000-06-29",
"prism:coverDisplayDate": "29 June 2000",
"prism:doi": "10.1038/35016500",
"citedby-count": "1386",
"affiliation": [
{
"@_fa": "true",
"affilname": "Stanford University",
"affiliation-city": "Palo Alto",
"affiliation-country": "United States"
}
],
"pubmed-id": "10890435",
"prism:aggregationType": "Journal",
"subtype": "re",
"subtypeDescription": "Review",
"source-id": "21206"
}
]
}
}
基本上,我想要检索"citedby-count"
值为1386,并且链接中的网址为["@ref"] == "scopus-citedby"
即"https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
。所以预期的输出应该像这样返回:
{
"citationCount": 1386,
"linkBack": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
}
我对上述代码的错误是:
citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
TypeError: list indices must be integers
如何解决TypeError: list indices must be integers
问题?
提前致谢。
答案 0 :(得分:1)
您使用了字符串索引而不是整数索引 变化
citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
到
citationCount = serviceResponseObject["search-results"]["entry"][0]["citedby-count"]
答案 1 :(得分:1)
serviceResponseObject [“search-results”] [“entry”]是一个列表,列表中的索引必须是整数
将serviceResponseObject["search-results"]["entry"]["0"]
更改为
serviceResponseObject["search-results"]["entry"][0]