-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_get_citing_articles.py
220 lines (196 loc) · 8.55 KB
/
05_get_citing_articles.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
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.command import Command
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
import os
import sys
DOWNLOAD_PATH = os.getcwd() + '/search_results/citing_articles'
WEB_SCIENCE_USERNAME = ""
WEB_SCIENCE_PASSWORD = ""
LOGIN_URL = ('https://ezpa.library.ualberta.ca/ezpAuthen.cgi'
'?url=https://www.webofscience.com/wos/alldb/basic-search')
dt = pd.read_csv(os.getcwd()+'/selected_records.csv')
try:
start = int(sys.argv[1])
except Exception as e:
start = 0
while True:
if start >= len(dt):
break
try:
# Set the default download folder
chrome_driver = os.path.abspath("chromedriver")
os.environ["webdriver.chrome.driver"] = chrome_driver
options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
#options.add_argument("--disable-gpu")
#options.add_argument("--no-sandbox") # linux only
#options.add_argument("--headless")
#options.add_argument("--remote-debugging-port=9222")
#options.add_argument("--window-size=1280,1024")
options.add_argument("--window-size=1024,760")
default_download_path = {"download.default_directory": DOWNLOAD_PATH}
options.add_experimental_option("prefs", default_download_path)
browser = webdriver.Chrome(executable_path=chrome_driver,
options=options)
browser.implicitly_wait(5)
browser.get(LOGIN_URL)
elem = browser.find_element(By.CSS_SELECTOR, "input[name=user]")
elem.send_keys(WEB_SCIENCE_USERNAME)
elem = browser.find_element(By.CSS_SELECTOR, "input[name=pass]")
elem.send_keys(WEB_SCIENCE_PASSWORD)
elem = browser.find_element(By.CSS_SELECTOR, "form[name=loginForm]")
elem.submit()
time.sleep(5)
# ESCAPE to close popup
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
try:
print('Wait for cookie popup')
loc = (By.XPATH, "//button[@id='onetrust-accept-btn-handler']")
wait = WebDriverWait(browser, 30)
wait.until(EC.visibility_of_element_located(loc))
print('Close Cookie popup')
# close cookie popup
xpath = "//button[@id='onetrust-accept-btn-handler']"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
except Exception as e:
print(e)
first_search = True
for i in range(0, 200):
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
if start >= len(dt):
break
a = dt['Article Title'][start]
b = dt['name_each_file'][start]
print(start)
ofn = DOWNLOAD_PATH + '/' + str(b) + '.tsv'
if os.path.exists(ofn):
start += 1
continue
try:
# "Advanced Search" click
elem = browser.find_element(By.LINK_TEXT, "Advanced Search")
elem.click()
# Wait until the `advancedSearchInputArea` element appear
# (up to 5 seconds)
wait = WebDriverWait(browser, 5)
loc = (By.ID, "advancedSearchInputArea")
wait.until(EC.presence_of_element_located(loc))
# enter the query string in textarea
xpath = "//span[contains(., 'Clear')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
elem = browser.find_element(By.ID, "advancedSearchInputArea")
s = 'TI=(' + a + ')'
print(s)
elem.send_keys(s)
# click 'Search' button
xpath = "//button[contains(., 'Search')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
time.sleep(1)
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
print(browser.current_url)
xpath = "//div[contains(@class, 'stats-section-section')]/div"
elems = browser.find_elements(By.XPATH, xpath)
stats_count = len(elems)
print('Stats Count = %d' % stats_count)
if stats_count < 2:
print('No citations')
start += 1
continue
# click 'Citations'
try:
print('Wait for Citations')
xpath = "//div[contains(@class, 'citations')]"
loc = (By.XPATH, xpath)
wait = WebDriverWait(browser, 2)
wait.until(EC.presence_of_element_located(loc))
except Exception as e:
print(e)
print('No citations')
start += 1
continue
try:
xpath = "//div[contains(@class, 'citations')]/a"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
except Exception as e:
print(e)
print('No citations')
start += 1
continue
loc = (By.XPATH, "//button[contains(., 'Export')]")
wait = WebDriverWait(browser, 5)
wait.until(EC.presence_of_element_located(loc))
# click 'Export' dropdown button
xpath = "//button[contains(., 'Export')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
xpath = "//button[contains(., 'Tab delimited file')]"
loc = (By.XPATH, xpath)
wait = WebDriverWait(browser, 5)
wait.until(EC.presence_of_element_located(loc))
try:
xpath = "//button[contains(., 'Tab delimited file')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
print('Click on Tab delimeted file')
except Exception as e:
print(e)
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
elem.click()
print('Click on Tab delimted file (again)')
xpath = "//button[contains(., 'Author, Title, Source')]"
loc = (By.XPATH, xpath)
wait = WebDriverWait(browser, 5)
wait.until(EC.presence_of_element_located(loc))
xpath = "//button[contains(., 'Author, Title, Source')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
# tick to 'Records from:'
xpath = "//span[contains(., 'Records from:')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
# export file
xpath = "//span[contains(., 'Export')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
dfn = DOWNLOAD_PATH + '/' + 'savedrecs.txt'
r = 0
while (r < 20):
print('Downloading...')
time.sleep(1)
if os.path.exists(dfn):
os.rename(dfn, ofn)
break
r += 1
if r == 20:
xpath = "//span[contains(., 'Cancel')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
print('ERROR: Cannot download', start)
except Exception as e:
print(e)
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
try:
txt = "< BACK TO BASIC SEARCHES"
elem = browser.find_element(By.LINK_TEXT, txt)
elem.click()
time.sleep(1)
# ESCAPE to close popup (if any)
webdriver.ActionChains(browser).send_keys(Keys.ESCAPE).perform()
except Exception as e:
print(e)
start += 1
except Exception as e:
print(e)
finally:
browser.quit()