在google scholar中循环列出25位缺少引用的作者

2024-05-02 21:41:01 发布

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

我正在使用from scholarly import scholarly从谷歌学者那里获取教授列表中25位教授的引用次数。然而,有些作者没有引文。我如何能够循环并停止迭代。我已尝试使用以下代码:

for i in professorlist:
    if i in professorlist:
        a = scholarly.search_author(i)
author = next(search_query)
scholarly.pprint(scholarly.fill(author, sections=['counts']))
if i not in professorlist:
    print ("no citations")

但我得到了这个错误:

---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-636-b3aa9d644e9f> in <module>
      2     if i in professorlist:
      3         a = scholarly.search_author(i)
----> 4 author = next(search_query)
      5 scholarly.pprint(scholarly.fill(author, sections=['counts']))
      6 if i not in professorlist:

StopIteration:

Tags: insearchifnotqueryfillauthornext
2条回答

对于搜索查询中的作者: scholarly.pprint(scholarly.fill(author,sections=['counts'])) 通过

谢谢:)你是说像这样吗@

它似乎通过了,但没有返回任何结果

当您使用生成器时,您可以通过调用next方法获得项,就像您在提供的代码段中所做的那样;但是,如果在耗尽/空的生成器上调用此方法,则会出现StopIteration错误

您可以通过如下方式将其包装到try/except块中来管理它

try:
    author = next(search_query)
except StopIteration:
    print("Iterated over all authors")
else:
    # do something with your author
    pass

您还可以使用for循环迭代生成器;它将自动处理StopIteration错误,在生成器耗尽时退出

for author in search_query:
    # do something with your author
    pass

相关问题 更多 >