-
Notifications
You must be signed in to change notification settings - Fork 2
/
spiderer.py
415 lines (329 loc) · 12.4 KB
/
spiderer.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
def spiderer(journal, publisher):
if publisher == 'apa':
apa(journal = journal)
if publisher == 'elsevier':
elsevier(journal = journal)
if publisher == 'sage':
sage(journal = journal)
if publisher == 'springer':
springer(journal = journal)
if publisher == 'taylorfrancis':
taylorfrancis(journal = journal)
if publisher == 'wiley':
wiley(journal = journal)
###
def apa(journal):
import re
from time import sleep
import numpy as np
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Open firefox
driver = webdriver.Firefox()
# Navigate to tilburg worldcat
driver.get("https://tilburguniversity.on.worldcat.org/discovery")
# Search for journal ISSN
elem = driver.find_element_by_xpath('//*[@id="query"]')
elem.send_keys(journal)
elem.send_keys(Keys.RETURN)
sleep(6)
# Open the journal
try:
elem = driver.find_element_by_xpath('//*[contains(@id, "full-text-button-")]')
sleep(1)
elem.click()
driver.switch_to_window(driver.window_handles[-1])
title=driver.title
except (selenium.common.exceptions.ElementNotVisibleException, selenium.common.exceptions.NoSuchElementException):
print "Failed APA %s" % journal
raise ValueError
try:
elem = driver.find_element_by_xpath('//*[@id="link_fulltext_1"]')
sleep(1)
elem.click()
except (selenium.common.exceptions.ElementNotVisibleException, selenium.common.exceptions.NoSuchElementException):
print "Failed APA %s" % journal
raise ValueError
try:
elem = driver.find_element_by_xpath('//*[@id="lnkPageOptions"]')
sleep(1)
elem.click()
except (selenium.common.exceptions.ElementNotVisibleException, selenium.common.exceptions.NoSuchElementException):
print "Failed APA %s" % journal
raise ValueError
try:
elem = driver.find_element_by_xpath('//*[@id="pageOptions"]/li[3]/ul/li[6]/a/span[2]')
sleep(1)
elem.click()
except (selenium.common.exceptions.ElementNotVisibleException, selenium.common.exceptions.NoSuchElementException):
print "Failed APA %s" % journal
raise ValueError
links = []
i = 1
while True:
i += 1
if (i % 15) == 0:
sleep(np.random.poisson(20))
x1 = len(links)
elem = driver.find_elements_by_xpath('//*[contains(@id, "htmlft")]')
for each in elem:
links.append(each.get_attribute('href'))
print "New number of links: %s" % (len(links) - x1)
try:
nextbutton = driver.find_element_by_xpath('//*[@id="ctl00_ctl00_MainContentArea_MainContentArea_bottomMultiPage_lnkNext"]')
nextbutton.click()
sleep(np.random.poisson(1))
except selenium.common.exceptions.NoSuchElementException:
driver.close()
break
np.savetxt("journal-links/apa_%s.csv" % journal, links, fmt = "%s")
###
def elsevier(journal):
import numpy as np
import re
from get_links import process
if not len(str(journal)) == 8:
journal = '0' * (8 - len(str(journal))) + journal
# lvl1 is the base url
lvl1 = 'http://www.sciencedirect.com/science/journal/%s' % journal
# links to recognize at lvl1 for lvl2
lvl1_recog = '(http://www\.sciencedirect\.com/science/journal/%s/[0-9]{1,3})(?!/.*)$' % journal
# links to recognize at lvl2 for lvl3
lvl2_recog = 'http://www\.sciencedirect\.com/science/journal/%s(/[0-9]{1,3})?/[0-9]{1,}(?!#maincontent)$' %journal
# links to recognize at lvl3 for lvl4
lvl3_recog = '(http://www\.sciencedirect\.com/science/article/pii/S?%s[A-Z0-9]{8,})(?!.*main.pdf$)' %journal
# get all links from lvl1 (in an array)
lvl2_unselect = np.array(process(lvl1))
# select only the lvl2 recognized links
r = re.compile(lvl1_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl2_unselect = np.sort(lvl2_unselect[vmatch(lvl2_unselect)]) # make sure they are sorted
# Remove duplicates
lvl2_unselect = np.sort(list(set(lvl2_unselect)))
# Retrieve all pages that contain the separate volumes and issues
lvl2 = []
for link in lvl2_unselect:
temp = int(link[link.rfind('/') + 1:])
if not temp % 10:
lvl2.append(link)
if link == lvl2_unselect[-1]:
lvl2.append(link)
# Sort for ease of use
lvl2 = np.sort(lvl2)
lvl3 = []
for link in lvl2:
# get all links from lvl3 (in an array)
lvl3_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl2_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl3.append(np.sort(lvl3_unselect[vmatch(lvl3_unselect)]))
print "Still working on lvl3 extraction, %s" % link
# fit all results of lvl3 into one array instead of multiple
lvl3 = np.concatenate(lvl3)
lvl3 = np.unique(lvl3)
lvl4 = []
for link in lvl3:
# get all links from lvl4 (in an array)
lvl4_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl3_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl4.append(np.sort(lvl4_unselect[vmatch(lvl4_unselect)]))
print "Still working on lvl4 extraction, %s" % link
# fit all results of lvl4 into one array instead of multiple
lvl4 = np.concatenate(lvl4)
np.savetxt("journal-links/elsevier_%s.csv" % journal, lvl4, fmt = "%s")
###
def sage(journal):
import numpy as np
import re
from get_links import process
# lvl1 is the base url
lvl1 = 'http://%s.sagepub.com/content/by/year/' % journal
# links to recognize at lvl1 for lvl2
lvl1_recog = 'http://%s.sagepub.com/content/by/year/[0-9]{4}' % journal
# links to recognize at lvl2 for lvl3
lvl2_recog = 'http://%s.sagepub.com/content/vol[0-9]{1,}/issue[0-9]{1,}/' % journal
# links to recognize at lvl3 for lvl4
lvl3_recog = 'http://%s.sagepub.com/content/[0-9]{1,}/[0-9]{1,}/[0-9]{1,}.full(?!.pdf+html)$' % journal
# get all links from lvl1 (in an array)
lvl2_unselect = np.array(process(lvl1))
# select only the lvl2 recognized links
r = re.compile(lvl1_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl2 = np.sort(lvl2_unselect[vmatch(lvl2_unselect)]) # make sure they are sorted
# create lvl3 object to append to
lvl3 = []
for link in lvl2:
# get all links from lvl3 (in an array)
lvl3_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl2_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl3.append(np.sort(lvl3_unselect[vmatch(lvl3_unselect)]))
print "Still working on lvl3 extraction, %s" % link
# fit all results of lvl3 into one array instead of multiple
lvl3 = np.concatenate(lvl3)
# create lvl3 object to append to
lvl4 = []
for link in lvl3:
# get all links from lvl4 (in an array)
lvl4_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl3_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl4.append(np.sort(lvl4_unselect[vmatch(lvl4_unselect)]))
print "Still working on lvl4 extraction, %s" % link
# fit all results of lvl4 into one array instead of multiple
lvl4 = np.concatenate(lvl4)
np.savetxt("journal-links/sage_%s.csv" % journal, lvl4, fmt = "%s")
###
def springer(journal):
import numpy as np
import re
from get_links import process
# lvl1 is the base url
lvl1 = 'http://link.springer.com/journal/volumesAndIssues/%s' % journal
# links to recognize at lvl1 for lvl2
lvl1_recog = 'http://link.springer.com/journal/[0-9]{1,}/[0-9]{1,}/[0-9]{1,}/page/[0-9]{1,}'
# links to recognize at lvl2 for lvl3
lvl2_recog = 'http://link\.springer\.com/article/[0-9]{2}\.[0-9]{4}/(?!.*fulltext\.html$).*'
# get all links from lvl1 (in an array)
lvl2_unselect = np.array(process(lvl1))
# select only the lvl2 recognized links
r = re.compile(lvl1_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl2 = np.sort(lvl2_unselect[vmatch(lvl2_unselect)]) # make sure they are sorted
# create lvl3 object to append to
lvl3 = []
for link in lvl2:
# get all links from lvl3 (in an array)
lvl3_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl2_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl3.append(np.sort(lvl3_unselect[vmatch(lvl3_unselect)]))
i = 1
x = "continue"
while x == "continue":
s = list(link)
s[-1] = str(i + 1)
link = "".join(s)
lvl3_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl2_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
temp = np.sort(lvl3_unselect[vmatch(lvl3_unselect)])
if temp.size == 0:
x = "stop"
if x == "continue":
lvl3.append(temp)
i += 1
print "Still working on lvl3 extraction, %s" % link
# fit all results of lvl3 into one array instead of multiple
lvl3 = np.concatenate(lvl3)
np.savetxt("journal-links/springer_%s.csv" % journal, lvl3, fmt = "%s")
###
def taylorfrancis(journal):
import re
from time import sleep
import numpy as np
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Open journal page
driver = webdriver.Firefox()
driver.get("http://www.tandfonline.com/loi/%s" % journal)
# Open all volume indexers
select = 'True'
while select == 'True':
for i in xrange(1,999):
try:
elem = driver.find_element_by_xpath('//*[@id="unit2"]/div[2]/div[2]/ul/li[%s]/div/a[2]' % i)
elem.click()
sleep(np.random.poisson(2))
except selenium.common.exceptions.NoSuchElementException:
select = 'False'
# Get list of issues
try:
elem = driver.find_elements_by_xpath('//*[@id="unit2"]/div[2]/div[2]/ul/li[*]/div[*]/div[1]/a')
except selenium.common.exceptions.StaleElementReferenceException:
print "ARGH"
issues = []
for issue in elem:
issues.append(str(issue.get_attribute('href')))
driver.close()
# Open each issue
issuesfulltext = []
for link in issues:
driver = webdriver.Firefox()
driver.get(link)
# Find full text htmls
try:
elem = driver.find_elements_by_xpath('//*[@id="unit2"]/form/div[*]/div/div[2]/a')
fulltext = []
for html in elem:
fulltext.append(str(html.get_attribute('href')))
except selenium.common.exceptions.StaleElementReferenceException:
print "Fail"
sleep(np.random.poisson(10))
driver.close()
sleep(np.random.poisson(10))
print "Still working on issue extraction, %s" % link
fulltext = np.array(fulltext)
fulltext_recog = "http://www.tandfonline.com/doi/full/"
r = re.compile(fulltext_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
issuesfulltext.append(fulltext[vmatch(fulltext)])
issuesfulltext = np.concatenate(issuesfulltext)
np.savetxt("journal-links/taylorfrancis_%s.csv" % journal, issuesfulltext, fmt = "%s")
###
def wiley(journal):
import numpy as np
import re
from get_links import process
journal_recog = journal[:journal.index('(')] + '(' + journal[journal.index('(') + 1:journal.index(')')] + ')' + journal[journal.index(')') + 1:]
lvl1 = 'http://onlinelibrary.wiley.com/journal/%s/issues' % journal
# links to recognize at lvl1 for lvl2
lvl1_recog = 'http://onlinelibrary.wiley.com/journal/%s/issues\\?activeYear=[0-9]{4}' % re.escape(journal)
# links to recognize at lvl2 for lvl3
lvl2_recog = 'http://onlinelibrary.wiley.com/doi/[0-9]{2}.[0-9]{4}/.*/issuetoc'
# links to recognize at lvl3 for lvl4
lvl3_recog = 'http://onlinelibrary.wiley.com/doi/[0-9]{2}.[0-9]{4}/.*/full'
# get all links from lvl1 (in an array)
lvl2_unselect = np.array(process(lvl1))
# Remove duplicates
lvl2_unselect = np.sort(list(set(lvl2_unselect)))
# select only the lvl2 recognized links
r = re.compile(lvl1_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl2 = np.sort(lvl2_unselect[vmatch(lvl2_unselect)]) # make sure they are sorted
# create lvl3 object to append to
lvl3 = []
for link in lvl2:
# get all links from lvl3 (in an array)
lvl3_unselect = np.array(process(link))
# select only the lvl3 recognized links
r = re.compile(lvl2_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl3.append(np.sort(lvl3_unselect[vmatch(lvl3_unselect)]))
print "Still working on lvl3 extraction, %s" % link
# fit all results of lvl3 into one array instead of multiple
lvl3 = np.concatenate(lvl3)
# create lvl4 object to append to
lvl4 = []
for link in lvl3:
# get all links from lvl4 (in an array)
lvl4_unselect = np.array(process(link))
# select only the lvl4 recognized links
r = re.compile(lvl3_recog)
vmatch = np.vectorize(lambda x:bool(r.match(x)))
lvl4.append(np.sort(lvl4_unselect[vmatch(lvl4_unselect)]))
print "Still working on lvl4 extraction, %s" % link
# fit all results of lvl4 into one array instead of multiple
lvl4 = np.concatenate(lvl4)
journal_save = re.sub('/', '', re.sub('\(', '', re.sub('\)', '', journal)))
np.savetxt("journal-links/wiley_%s.csv" % journal_save, lvl4, fmt = "%s")
###