python - 使用 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 函数来获取字符串)库从每条记录中检索结果,但我想获得结果的总数。标签是什么还是有其他方法?
解决方案
作为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 工作。
推荐阅读
- python - 在 Python 中使用 subprocess.run 捕获子进程脚本的完整输出
- javascript - 为什么我的 .collapse("show") & .collapse("hide") 不起作用?
- javascript - 我无法在页面上显示 vue-chart.js
- reactjs - React JS Chart JS 2 没有在后台隐藏网格线
- rust - 预期结构,但发现对结构的可变引用
- javascript - Mapbox-gl-js 生成不同的图标
- python - 条件为 True 时重新加载 dash 应用程序的方法
- python - 将xlsx文件转换为csv文件时如何修复python3中的__init__ typeError?
- r - 如何使用 GenomicRanges 制作序列图
- kotlin - 为什么尝试在 GoogleMap 上放置标记时出现 NullPointerException?