将PythonScopus API结果导出到CSV

2024-04-29 06:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python很陌生,所以不确定是否可以做到,但我希望可以!在

我访问了Scopus API并成功运行了一个搜索查询,在pandas数据框中得到以下结果:

                                                            search-results
entry                    [{'@_fa': 'true', 'affiliation': [{'@_fa': 'tr...
link                     [{'@_fa': 'true', '@ref': 'self', '@type': 'ap...
opensearch:Query         {'@role': 'request', '@searchTerms': 'AFFIL(un...
opensearch:itemsPerPage                                                200
opensearch:startIndex                                                    0
opensearch:totalResults                                             106652

如果可能的话,我想把106652的结果导出到一个csv文件中,这样就可以对它们进行分析了。这可能吗?在


Tags: 数据refapitruepandassearchlinkopensearch
1条回答
网友
1楼 · 发布于 2024-04-29 06:13:16

首先你需要得到所有的结果(见下面的评论)。 您需要的数据(搜索结果)在“条目”列表中。 您可以提取该列表并将其附加到支持列表中,反复迭代直到获得所有结果。在这里我循环,并且在每一轮我从结果的总数中减去下载的项目(计数)。在

        found_items_num = 1
        start_item = 0
        items_per_query = 25
        max_items = 2000
        JSON = []

        print ('GET data from Search API...')

        while found_items_num > 0:

            resp = requests.get(self._url,
                                headers={'Accept': 'application/json', 'X-ELS-APIKey': MY_API_KEY},
                                params={'query': query, 'view': view, 'count': items_per_query,
                                        'start': start_item})

            print ('Current query url:\n\t{}\n'.format(resp.url))

            if resp.status_code != 200:
                # error
                raise Exception('ScopusSearchApi status {0}, JSON dump:\n{1}\n'.format(resp.status_code, resp.json()))

            # we set found_items_num=1 at initialization, on the first call it has to be set to the actual value
            if found_items_num == 1:
                found_items_num = int(resp.json().get('search-results').get('opensearch:totalResults'))
                print ('GET returned {} articles.'.format(found_items_num))

            if found_items_num == 0:
                pass
            else:
                # write fetched JSON data to a file.
                out_file = os.path.join(str(start_item) + '.json')

                with open(out_file, 'w') as f:
                    json.dump(resp.json(), f, indent=4)
                    f.close()

                # check if results number exceed the given limit
                if found_items_num > max_items:
                    print('WARNING: too many results, truncating to {}'.format(max_items))
                    found_items_num = max_items



                # check if returned some result
                if 'entry' in resp.json().get('search-results', []):
                    # combine entries to make a single JSON
                    JSON += resp.json()['search-results']['entry']
            # set counters for the next cycle
            self._found_items_num -= self._items_per_query
            self._start_item += self._items_per_query
            print ('Still {} results to be downloaded'.format(self._found_items_num if self._found_items_num > 0 else 0))

        # end while - finished downloading JSON data

然后,在while之外,你可以像这样保存完整的文件。。。在

^{pr2}$

或者您可以按照this guide i found online(未测试,可以搜索“json to cvs python”并获得许多指南)将json数据转换为csv

相关问题 更多 >