-
Notifications
You must be signed in to change notification settings - Fork 0
/
webofsciencescraper.py
291 lines (235 loc) · 8.65 KB
/
webofsciencescraper.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
291
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium import webdriver
import bibtexparser
import os
import re
import json
import argparse
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class PaperData:
author = ''
paper = ''
universities = ''
parentId = ''
year = ''
citedReferences = ''
timesCited = ''
def to_JSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
def __init__(self, universities, parentId, author, paper, year, citedReferences, timesCited):
self.citedReferences = citedReferences
self.universities = universities
self.year = year
self.timesCited = str(timesCited)
self.paper = paper
if parentId == None:
self.parentId = None
else:
self.parentId = parentId
self.author = author
def getTimesCited(self):
return self.timesCited
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getPaper(self):
return self.paper
def getUniversities(self):
return self.universities
def __str__(self):
final_string = "---------PAGE------------\n"
final_string += "Paper Name: " + self.paper + "\n"
final_string += "Paper Authors:" + self.author + "\n"
if (self.universities):
final_string += "Research Location: " + ', ' + self.universities + "\n"
final_string += "Who does it cite?\n"
if (self.citedReferences):
final_string += self.citedReferences
final_string += "Year published: " + self.year + "\n"
final_string += "Times Cited: " + str(self.timesCited) + "\n"
if self.parentId != None:
final_string += "Parent Node Identification: " + str(self.parentId) + "\n"
else:
final_string += "Parent Node Identification: Root Node\n"
final_string += "----------END-------------\n"
return final_string
def timesCited(entry):
times_cited = ''
try:
times_cited = str(entry['times-cited'])
except KeyError:
return None
return times_cited
def downloadFromList(driver, FROM, TO):
selector2 = driver.find_element_by_xpath("//select[@name='saveToMenu']")
options_for_second_selector = selector2.find_elements_by_tag_name("option")
for option in options_for_second_selector:
if (option.get_attribute('value') == 'other'):
option.click()
break
radio_element = driver.find_element_by_name("value(record_select_type)")
radio_element.click()
from_element = driver.find_element_by_id("markFrom")
from_element.send_keys(str(FROM))
to_element = driver.find_element_by_id("markTo")
to_element.send_keys(str(TO))
select = Select(driver.find_element_by_id('bib_fields'))
select.select_by_index(3)
select = Select(driver.find_element_by_id('saveOptions'))
select.select_by_index(1)
thing = driver.find_element_by_class_name("quickoutput-action")
thing.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, 'send'))
)
exitbutton = driver.find_element_by_class_name("quickoutput-cancel-action")
exitbutton.click()
def find_university(entry):
try:
full_string = str(entry['affiliation']).lower()
except KeyError:
try:
full_string = str(entry['Affiliation']).lower()
except KeyError:
return None
full_list = full_string.split(", ")
new_b = None
done = False
for aff in full_list:
sub_list = aff.split(".\n")
for b in sub_list:
if ("univ" in b or "corp" in b or "coll" in b or "acad" in b or "inst" in b or " inc" in b or " llc" in b):
return b
def find_authors(entry):
try:
full_string = str(entry['Author'])
except KeyError:
try:
full_string = str(entry['author'])
except KeyError:
return None
return full_string.replace(" and ", ";")
def find_year(entry):
try:
full_string = str(entry['Year'])
except KeyError:
try:
full_string = str(entry['year'])
except KeyError:
return None
return full_string
def find_title(entry):
try:
full_string = str(entry['title'])
except KeyError:
try:
full_string = str(entry['Title'])
except KeyError:
return None
return full_string
def bibtex_parsing(fileName, parent):
list_of_instances = []
with open(fileName) as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
for entry in bib_database.entries:
list_of_instances.append(
PaperData(find_university(entry), parent, find_authors(entry), find_title(entry), find_year(entry),
get_cited_references(entry), timesCited(entry)))
return list_of_instances
def pickleUpdateWithList(thelist, fileName):
file=open(fileName, 'a')
for a in thelist:
if(os.stat(fileName).st_size == 0):
file.write("[")
else:
file.write(",")
file.write(a.to_JSON());
file.close()
def store_data(fileNameToAppend, desiredLocation, parent):
list_of_indexes = bibtex_parsing(fileNameToAppend, parent)
pickleUpdateWithList(list_of_indexes, desiredLocation)
def removeFile(path):
try:
os.remove(path)
except:
print "no path to remove"
def error_filter(path, destination, parent, driver, start, end):
print start, end
removeFile(path)
downloadFromList(driver, str(start), str(end))
time.sleep(3)
try:
store_data(path, destination, parent)
except:
if (end - start > 10):
error_filter(path, destination, parent, driver, start, start + int((end - start) / 2))
error_filter(path, destination, parent, driver, start + int((end - start) / 2), end)
def grab_data(driver, parent, destination):
number_of_files = int(get_number_of_files(driver))
number_of_pages = float(float(number_of_files) / 500.0)
if int(number_of_pages) != number_of_pages:
number_of_pages += 1
print "Number of pages"+str(int(number_of_pages))
number_of_pages = int(number_of_pages)
for i in range(0, number_of_pages):
print "Retrieving ~500 Files...",
time.sleep(2)
if (number_of_files >= 500 + (500 * i)):
downloadFromList(driver, str(1 + (500 * i)), str(500 + (500 * i)))
if (number_of_files < 500 + (500 * i)):
downloadFromList(driver, str(1 + (500 * i)), str(number_of_files))
print bcolors.OKBLUE + "Files Retrieved" + bcolors.ENDC,
time.sleep(3)
path = "/Users/aidancurtis/Downloads/savedrecs.bib"
try:
store_data(path, destination, parent)
except:
print bcolors.FAIL + "500 files discarded" + bcolors.ENDC
if (number_of_files >= 500 + (500 * i)):
removeFile(path)
error_filter(path, destination, parent, driver, 1 + (500 * i), 500 + (500 * i))
if (number_of_files < 500 + (500 * i)):
removeFile(path)
error_filter(path, destination, parent, driver, 1 + (500 * i), number_of_files)
removeFile(path)
print bcolors.OKGREEN + "Files Stored" + bcolors.ENDC
def get_number_of_files(driver):
return driver.find_element_by_xpath("//span[@id='hitCount.top']").text.replace(',', '')
def get_cited_references(entry):
long_string = ''
try:
long_string = entry['Cited-References']
except:
try:
long_string = entry['cited-references']
except:
return None
return long_string
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process some strings')
parser.add_argument('URL', metavar='N', nargs='+',help='URL from web of science')
parser.add_argument('FILE', metavar='N', nargs='+',help='a destination file')
destination= parser.parse_args().FILE[0]
theURL=parser.parse_args().URL[0]
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get(theURL)
grab_data(driver, None, destination)
driver.quit()
file=open(destination, 'a')
file.write("]")
file.close()