这是Raphael Meudec 答案的附加解决方案,但使用beautifulsoup 来解决此问题。它包含与 Raphael 使用的几乎相同的代码和逻辑。
如果标头没有帮助,您需要将其与代理一起使用,否则,Google Scholar 将阻止请求,因为自动化脚本会发送请求。
为请求添加代理,假设您将使用requests 库,例如:
proxies = {
'http': os.getenv('HTTP_PROXY')
}
# Request will be like so:
requests.get('google scholar link', proxies=proxies)
代码(在线IDE中bs4文件夹下的full example -> bs4_author_citedby_results):
from bs4 import BeautifulSoup
import requests, lxml, os, json
headers = {
'User-agent':
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}
proxies = {
'http': os.getenv('HTTP_PROXY')
}
html = requests.get('https://scholar.google.com/citations?hl=en&user=m8dFEawAAAAJ', headers=headers, proxies=proxies).text
soup = BeautifulSoup(html, 'lxml')
# This is basically the same as Raphael Meudec suggested but using beautifulsoup
years = [graph_year.text for graph_year in soup.select('.gsc_g_t')]
citations = [graph_citation.text for graph_citation in soup.select('.gsc_g_a')]
for year, citation in zip(years,citations):
print(f'{year} {citation}\n')
# Part of the output:
'''
2007 24
2008 30
2009 46
'''
或者您可以通过在zip() for 循环中添加data.append() 来生成这样的 JSON 输出:
data = []
for year, citation in zip(years,citations):
data.append({
'year': year,
'citation': citation,
})
print(json.dumps(data, indent=2))
# Part of the output:
'''
[
{
"year": "2007",
"citation": "24"
},
{
"year": "2008",
"citation": "30"
}
]
'''
或者,您可以使用来自 SerpApi 的 Google Scholar Author Cited By API。这是一个付费 API,可免费试用 5,000 次搜索。
要集成的代码:
from serpapi import GoogleSearch
import os
params = {
"api_key": os.getenv("API_KEY"),
"engine": "google_scholar_author",
"author_id": "8Cuk5vYAAAAJ",
"hl": "en",
}
search = GoogleSearch(params)
results = search.get_dict()
for graph_results in results['cited_by']['graph']:
year = graph_results['year']
citations = graph_results['citations']
print(f'{year} {citations}\n')
# part of the output
# JSON output could be added in the same manner as with bs4 code.
'''
2007 24
2008 30
2009 46
'''
免责声明,我为 SerpApi 工作。