-
Notifications
You must be signed in to change notification settings - Fork 4
/
scholarly.py
290 lines (254 loc) · 12.4 KB
/
scholarly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""scholarly.py (based on "github.com/scholarly-python-package/scholarly package") """
from __future__ import absolute_import, division, print_function, unicode_literals
from bs4 import BeautifulSoup
import arrow
import bibtexparser
import codecs
import hashlib
import pprint
import random
import re
import requests
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from arXiv_bot import _SSLVERIFYCRT
_GOOGLEID = hashlib.md5(str(random.random()).encode('utf-8')).hexdigest()[:16]
_COOKIES = {'GSP': 'ID={0}:CF=4'.format(_GOOGLEID)}
_HEADERS = {
'accept-language': 'en-US,en',
'User-Agent': 'Chrome/74.0.3729.169 Mozilla/5.0 (X11; Linux x86_64) ' + \
'AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/41.0.2272.76 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml'
}
_HOST = 'https://scholar.google.com'
_AUTHSEARCH = '/citations?view_op=search_authors&hl=en&mauthors={0}'
_CITATIONAUTH = '/citations?user={0}&hl=en'
_CITATIONPUB = '/citations?view_op=view_citation&citation_for_view={0}'
_KEYWORDSEARCH = '/citations?view_op=search_authors&hl=en&mauthors=label:{0}'
_PUBSEARCH = '/scholar?q={0}'
_SCHOLARPUB = '/scholar?oi=bibs&hl=en&cites={0}'
_CITATIONAUTHRE = r'user=([\w-]*)'
_CITATIONPUBRE = r'citation_for_view=([\w-]*:[\w-]*)'
_SCHOLARCITERE = r'gs_ocit\(event,\'([\w-]*)\''
_SCHOLARPUBRE = r'cites=([\w-]*)'
_EMAILAUTHORRE = r'Verified email at '
_SESSION = requests.Session()
_PAGESIZE = 100
def _get_page(pagerequest):
"""Return the data for a page on scholar.google.com"""
# Note that we include a sleep to avoid overloading the scholar server
time.sleep(5+random.uniform(0, 5))
resp = _SESSION.get(pagerequest, headers=_HEADERS, cookies=_COOKIES, verify=_SSLVERIFYCRT)
if resp.status_code == 200:
return resp.text
if resp.status_code == 503:
raise Exception('Error: {0} {1}'.format(resp.status_code, resp.reason))
else:
raise Exception('Error: {0} {1} {2} {3}'.format(resp.status_code, resp.reason, resp.text, pagerequest))
def _get_soup(pagerequest):
"""Return the BeautifulSoup for a page on scholar.google.com"""
html = _get_page(pagerequest)
html = html.replace(u'\xa0', u' ')
return BeautifulSoup(html, 'html.parser')
def _search_scholar_soup(soup):
"""Generator that returns Publication objects from the search page"""
while True:
for row in soup.find_all('div', 'gs_or'):
yield Publication(row, 'scholar')
if soup.find(class_='gs_ico gs_ico_nav_next'):
url = soup.find(class_='gs_ico gs_ico_nav_next').parent['href']
soup = _get_soup(_HOST+url)
else:
break
def _search_citation_soup(soup):
"""Generator that returns Author objects from the author search page"""
while True:
for row in soup.find_all('div', 'gsc_1usr'):
yield Author(row)
next_button = soup.find(class_='gs_btnPR gs_in_ib gs_btn_half gs_btn_lsb gs_btn_srt gsc_pgn_pnx')
if next_button and 'disabled' not in next_button.attrs:
url = next_button['onclick'][17:-1]
url = codecs.getdecoder("unicode_escape")(url)[0]
soup = _get_soup(_HOST+url)
else:
break
class Publication(object):
"""Returns an object for a single publication"""
def __init__(self, __data, pubtype=None):
self.bib = dict()
self.source = pubtype
if self.source == 'citations':
self.bib['title'] = __data.find('a', class_='gsc_a_at').text
self.id_citations = re.findall(_CITATIONPUBRE, __data.find('a', class_='gsc_a_at')['data-href'])[0]
citedby = __data.find(class_='gsc_a_ac')
if citedby and not (citedby.text.isspace() or citedby.text == ''):
self.citedby = int(citedby.text)
year = __data.find(class_='gsc_a_h')
if year and year.text and not year.text.isspace() and len(year.text)>0:
self.bib['year'] = int(year.text)
elif self.source == 'scholar':
databox = __data.find('div', class_='gs_ri')
title = databox.find('h3', class_='gs_rt')
if title.find('span', class_='gs_ctu'): # A citation
title.span.extract()
elif title.find('span', class_='gs_ctc'): # A book or PDF
title.span.extract()
self.bib['title'] = title.text.strip()
if title.find('a'):
self.bib['url'] = title.find('a')['href']
authorinfo = databox.find('div', class_='gs_a')
self.bib['author'] = ' and '.join([i.strip() for i in authorinfo.text.split(' - ')[0].split(',')])
if databox.find('div', class_='gs_rs'):
self.bib['abstract'] = databox.find('div', class_='gs_rs').text
if self.bib['abstract'][0:8].lower() == 'abstract':
self.bib['abstract'] = self.bib['abstract'][9:].strip()
lowerlinks = databox.find('div', class_='gs_fl').find_all('a')
for link in lowerlinks:
if 'Import into BibTeX' in link.text:
self.url_scholarbib = link['href']
if 'Cited by' in link.text:
self.citedby = int(re.findall(r'\d+', link.text)[0])
self.id_scholarcitedby = re.findall(_SCHOLARPUBRE, link['href'])[0]
if __data.find('div', class_='gs_ggs gs_fl'):
self.bib['eprint'] = __data.find('div', class_='gs_ggs gs_fl').a['href']
self._filled = False
def fill(self):
"""Populate the Publication with information from its profile"""
if self.source == 'citations':
url = _CITATIONPUB.format(self.id_citations)
soup = _get_soup(_HOST+url)
self.bib['title'] = soup.find('div', id='gsc_vcd_title').text
if soup.find('a', class_='gsc_vcd_title_link'):
self.bib['url'] = soup.find('a', class_='gsc_vcd_title_link')['href']
for item in soup.find_all('div', class_='gs_scl'):
key = item.find(class_='gsc_vcd_field').text
val = item.find(class_='gsc_vcd_value')
if key == 'Authors':
self.bib['author'] = ' and '.join([i.strip() for i in val.text.split(',')])
elif key == 'Journal':
self.bib['journal'] = val.text
elif key == 'Volume':
self.bib['volume'] = val.text
elif key == 'Issue':
self.bib['number'] = val.text
elif key == 'Pages':
self.bib['pages'] = val.text
elif key == 'Publisher':
self.bib['publisher'] = val.text
elif key == 'Publication date':
self.bib['year'] = arrow.get(val.text).year
elif key == 'Description':
if val.text[0:8].lower() == 'abstract':
val = val.text[9:].strip()
self.bib['abstract'] = val
elif key == 'Total citations':
self.id_scholarcitedby = re.findall(_SCHOLARPUBRE, val.a['href'])[0]
if soup.find('div', class_='gsc_vcd_title_ggi'):
self.bib['eprint'] = soup.find('div', class_='gsc_vcd_title_ggi').a['href']
self._filled = True
elif self.source == 'scholar':
bibtex = _get_page(self.url_scholarbib)
self.bib.update(bibtexparser.loads(bibtex).entries[0])
self._filled = True
return self
def get_citedby(self):
"""Searches GScholar for other articles that cite this Publication and
returns a Publication generator.
"""
if not hasattr(self, 'id_scholarcitedby'):
self.fill()
if hasattr(self, 'id_scholarcitedby'):
url = _SCHOLARPUB.format(requests.utils.quote(self.id_scholarcitedby))
soup = _get_soup(_HOST+url)
return _search_scholar_soup(soup)
else:
return []
def __str__(self):
return pprint.pformat(self.__dict__)
class Author(object):
"""Returns an object for a single author"""
def __init__(self, __data):
if isinstance(__data, str):
self.id = __data
else:
self.id = re.findall(_CITATIONAUTHRE, __data('a')[0]['href'])[0]
self.url_picture = _HOST+'/citations?view_op=medium_photo&user={}'.format(self.id)
self.name = __data.find('h3', class_='gs_ai_name').text
affiliation = __data.find('div', class_='gs_ai_aff')
if affiliation:
self.affiliation = affiliation.text
email = __data.find('div', class_='gs_ai_eml')
if email:
self.email = re.sub(_EMAILAUTHORRE, r'@', email.text)
self.interests = [i.text.strip() for i in
__data.find_all('a', class_='gs_ai_one_int')]
citedby = __data.find('div', class_='gs_ai_cby')
if citedby and citedby.text != '':
self.citedby = int(citedby.text[9:])
self._filled = False
def fill(self):
"""Populate the Author with information from their profile"""
url_citations = _CITATIONAUTH.format(self.id)
url = '{0}&pagesize={1}'.format(url_citations, _PAGESIZE)
soup = _get_soup(_HOST+url)
self.name = soup.find('div', id='gsc_prf_in').text
self.affiliation = soup.find('div', class_='gsc_prf_il').text
self.interests = [i.text.strip() for i in soup.find_all('a', class_='gsc_prf_inta')]
# h-index, i10-index and h-index, i10-index in the last 5 years
index = soup.find_all('td', class_='gsc_rsb_std')
if index:
self.citedby = int(index[0].text)
self.citedby5y = int(index[1].text)
self.hindex = int(index[2].text)
self.hindex5y = int(index[3].text)
self.i10index = int(index[4].text)
self.i10index5y = int(index[5].text)
else:
self.hindex = self.hindex5y = self.i10index = self.i10index5y = 0
# number of citations per year
years = [int(y.text) for y in soup.find_all('span', class_='gsc_g_t')]
cites = [int(c.text) for c in soup.find_all('span', class_='gsc_g_al')]
self.cites_per_year = dict(zip(years, cites))
self.publications = list()
pubstart = 0
while True:
for row in soup.find_all('tr', class_='gsc_a_tr'):
new_pub = Publication(row, 'citations')
self.publications.append(new_pub)
if 'disabled' not in soup.find('button', id='gsc_bpf_more').attrs:
pubstart += _PAGESIZE
url = '{0}&cstart={1}&pagesize={2}'.format(url_citations, pubstart, _PAGESIZE)
soup = _get_soup(_HOST+url)
else:
break
self._filled = True
return self
def __str__(self):
return pprint.pformat(self.__dict__)
def search_pubs_query(query):
"""Search by scholar query and return a generator of Publication objects"""
url = _PUBSEARCH.format(requests.utils.quote(query))
soup = _get_soup(_HOST+url)
return _search_scholar_soup(soup)
def search_author(name):
"""Search by author name and return a generator of Author objects"""
url = _AUTHSEARCH.format(requests.utils.quote(name))
soup = _get_soup(_HOST+url)
return _search_citation_soup(soup)
def search_keyword(keyword):
"""Search by keyword and return a generator of Author objects"""
url = _KEYWORDSEARCH.format(requests.utils.quote(keyword))
soup = _get_soup(_HOST+url)
return _search_citation_soup(soup)
def search_pubs_custom_url(url):
"""Search by custom URL and return a generator of Publication objects
URL should be of the form '/scholar?q=...'"""
soup = _get_soup(_HOST+url)
return _search_scholar_soup(soup)
def search_author_custom_url(url):
"""Search by custom URL and return a generator of Publication objects
URL should be of the form '/citation?q=...'"""
soup = _get_soup(_HOST+url)
return _search_citation_soup(soup)