首页 > 解决方案 > 使用 BeautifulSoup 从谷歌学者中抓取网页

问题描述

在 Google Scholar 中搜索字符串“电子膝盖”,我检索到大约 14.000 个结果。这是链接: https ://scholar.google.com/scholar?start=10&q=electronic+knee&hl=it&as_sdt=8,5&as_ylo=2017&as_rr=1

是否可以通过python中的网络抓取获得结果编号(位于页面开头)?我正在使用 bs4(find_all 函数来获取字符串)库从每条记录中检索结果,但我想获得结果的总数。标签是什么还是有其他方法?

标签: pythonbeautifulsoup

解决方案


作为furas答案的替代解决方案,同样的事情可以通过使用来自 SerpApi的Google Scholar Organic Results API来实现。这是一个带有免费计划的付费 API。

这种情况的不同之处在于,当语言更改或没有显示页码时,您将获得正确数量的结果。例如,如果不显示页码,parts = item.text.split(' ')[4]来自furas 的答案将不再起作用,它将抛出IndexError

text = "Pagina 2 di circa 14.400 risultati (0,02 sec)".split(" ")[4]
broken_text = "14.400 risultati (0,02 sec)".split(" ")[4]

print(text, broken_text, sep="\n")

'''
14.400

broken_text = "14.400 risultati (0,02 sec)".split(" ")[4]
IndexError: list index out of range
'''

# The regular expression should be used to avoid such behavior. 

要集成的代码:

from serpapi import GoogleSearch

params = {
  "api_key": "YOUR_API_KEY",   # SerpApi API key
  "engine": "google_scholar",  # Google Scholar Organic results engine
  "q": "electronic knee",      # search query
  "hl": "it",                  # language
  "as_ylo": "2017"             # from year
}

search = GoogleSearch(params)
results = search.get_dict()

print(results["search_information"]["total_results"])  # always returns a total results

# 15700

免责声明,我为 SerpApi 工作。


推荐阅读