-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAS.py
271 lines (250 loc) · 11.2 KB
/
MAS.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class MicrosoftAcademicScraper():
driver = None
def __init__(self):
self.driver = webdriver.Firefox()
def search(self, url):
self.driver.get(url)
def get_papers(self):
# Get number of papers
time.sleep(2)
papers = self.driver.find_elements_by_class_name("primary_paper")
index = 0
max_index = len(papers)
next_page = True
while next_page:
for index in range(max_index):
paper = self.get_paper(papers, index)
yield paper
papers = self.go_back_results_page()
if index + 1 == max_index:
if self.go_to_next_page():
time.sleep(2)
papers = self.driver.find_elements_by_class_name("primary_paper")
max_index = len(papers)
index = 0
else:
next_page = False
break
def get_paper(self, papers, index):
# Enter page
papers[index].find_element_by_class_name("title").click()
time.sleep(2)
page_content = self.get_page_content()
return page_content
# Explore page
def get_page_content(self):
try:
title = self.driver.find_element_by_class_name("name").text
except Exception as e:
print(e)
title = "Title not available"
try:
year = self.driver.find_element_by_class_name("year").text
except Exception as e:
print(e)
year = "Year not available"
try:
pub_name = self.driver.find_element_by_class_name("pub-name").text
except Exception as e:
print(e)
pub_name = "Publication name not available"
try:
venue_details = self.driver.find_element_by_class_name("venueDetails").text
except Exception as e:
print(e)
venue_details = "Venue details not available"
try:
doi = self.driver.find_element_by_class_name("doiLink").text
except Exception as e:
print(e)
doi = "DOI not available"
try:
authors = self.driver.find_elements_by_class_name("author-item")
authors_aux = []
for author in authors:
if author.text != "":
authors_aux.append(author.text)
authors = authors_aux
except Exception as e:
print(e)
authors = ["Authors not available"]
references = None
citations = None
try:
stats = self.driver.find_elements_by_class_name("ma-statistics-item")
for stat in stats:
name = stat.find_element_by_class_name("name").text
if name == "References":
references = stat.find_element_by_class_name("count").text
elif name == "Citations":
citations = stat.find_element_by_class_name("count").text
except Exception as e:
print(e)
references = "References not availble"
citations = "Citations not available"
if references == None:
references = "References not availble"
if citations == None:
citations = "Citations not available"
try:
abstract = self.driver.find_element_by_xpath("//div[@class='caption']/following-sibling::p").text
except Exception as e:
print(e)
abstract = "Abstract not available"
def get_paper_aux(referenced_paper):
try:
referenced_title = referenced_paper.find_element_by_class_name("title").text
except Exception as e:
print(e)
referenced_title = "Title not available"
try:
referenced_citations = referenced_paper.find_element_by_class_name("citation").text
except Exception as e:
print(e)
referenced_citations = "Citations not available"
try:
referenced_publication = referenced_paper.find_element_by_class_name("publication")
except Exception as e:
print(e)
try:
referenced_year = referenced_publication.find_element_by_class_name("year").text
except Exception as e:
print(e)
referenced_year = "Year not available"
try:
referenced_pub_name = referenced_publication.find_element_by_class_name("name").text
except Exception as e:
print(e)
referenced_pub_name = "Pub name not available"
try:
referenced_authors = []
referenced_authors_aux = referenced_paper.find_elements_by_class_name("author-item")
for referenced_author_aux in referenced_authors_aux:
referenced_author = referenced_author_aux.text
referenced_authors.append(referenced_author)
except Exception as e:
print(e)
referenced_authors = ["Authors not available"]
try:
referenced_abstract = referenced_paper.find_element_by_class_name("ma-expandable-text")
try:
referenced_abstract.find_element_by_class_name("show-more").click()
except Exception as e:
print(e)
referenced_abstract = referenced_abstract.find_element_by_class_name("text").text
except Exception as e:
print(e)
referenced_abstract = "Abstract not available"
referenced_content = {
"title" : referenced_title,
"citations" : referenced_citations,
"year" : referenced_year,
"pub_name" : referenced_pub_name,
"authors" : referenced_authors,
"abstract" : referenced_abstract
}
return referenced_content
try:
tabs = self.driver.find_elements_by_class_name("route")
for tab in tabs:
if "active" in tab.get_attribute("class").split():
if tab.text == "REFERENCES":
referenced = []
referenced_papers = self.driver.find_elements_by_class_name("primary_paper")
for referenced_paper in referenced_papers:
referenced_content = get_paper_aux(referenced_paper)
referenced.append(referenced_content)
try:
tabs = self.driver.find_elements_by_class_name("route")
time.sleep(2)
tabs[3].click()
time.sleep(2)
cited = []
cited_papers = self.driver.find_elements_by_class_name("primary_paper")
for cited_paper in cited_papers:
cited_content = get_paper_aux(cited_paper)
cited.append(cited_content)
except Exception as e:
print(e)
cited = ["No cited"]
try:
tabs = self.driver.find_elements_by_class_name("route")
time.sleep(2)
tabs[5].click()
time.sleep(2)
related = []
related_papers = self.driver.find_elements_by_class_name("primary_paper")
for related_paper in related_papers:
related_content = get_paper_aux(related_paper)
related.append(related_content)
except Exception as e:
print(e)
related = ["No related"]
elif tab.text == "CITED BY":
referenced = ["No references"]
cited = []
cited_papers = self.driver.find_elements_by_class_name("primary_paper")
for cited_paper in cited_papers:
cited_content = get_paper_aux(cited_paper)
cited.append(cited_content)
try:
tabs = self.driver.find_elements_by_class_name("route")
time.sleep(2)
tabs[5].click()
time.sleep(2)
related = []
related_papers = self.driver.find_elements_by_class_name("primary_paper")
for related_paper in related_papers:
related_content = get_paper_aux(related_paper)
related.append(related_content)
except Exception as e:
print(e)
related = ["No related"]
elif tab.text == "RELATED":
referenced = ["No references"]
cited = ["No cited"]
related = []
related_papers = self.driver.find_elements_by_class_name("primary_paper")
for related_paper in related_papers:
related_content = get_paper_aux(related_paper)
related.append(related_content)
except Exception as e:
print(e)
content = {
"bib" : {
"title" : title,
"year" : year,
"pub_name" : pub_name,
"abstract" : abstract,
"venue_details" : venue_details,
"doi" : doi,
"authors" : authors,
"n_references" : references,
"n_citations" : citations
},
"references" : referenced,
"cited" : cited,
"related" : related
}
return content
def go_back_results_page(self):
self.driver.execute_script("window.history.go(-1)")
time.sleep(2)
papers = self.driver.find_elements_by_class_name("primary_paper")
return papers
def go_to_next_page(self):
time.sleep(2)
pager = self.driver.find_elements_by_class_name("page")
current = False
for next_page in pager:
if current:
next_page.click()
break
if "current" in next_page.get_attribute("class").split():
current = True
if next_page == pager[-1]:
return False
return True