-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_get_control_articles.py
233 lines (214 loc) · 9.16 KB
/
03_get_control_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
221
222
223
224
225
226
227
228
229
230
231
232
233
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
import pandas as pd
import time
import os
import calendar
import datetime
DOWNLOAD_PATH = os.getcwd() + '/search_results'
WEB_SCIENCE_USERNAME = ""
WEB_SCIENCE_PASSWORD = ""
LOGIN_URL = ('https://ezpa.library.ualberta.ca/ezpAuthen.cgi'
'?url=https://www.webofscience.com/wos/alldb/basic-search')
# Create query strings
dt = pd.read_csv(os.getcwd() + '/retracted_articles.csv.gz')
li_mon = []
for i in dt.PD:
if (type(i) == str) & (str(i).title() in list(calendar.month_abbr)):
li_mon.append(list(calendar.month_abbr).index(i.title()))
elif type(i) == datetime.datetime:
li_mon.append(i.month)
else:
li_mon.append(1)
dt['month'] = li_mon
li_date = []
for i in dt.PD:
if type(i) == datetime.datetime:
li_date.append(i.day)
else:
li_date.append(1)
dt['date'] = li_date
dt.dropna(subset=['PY'], inplace=True)
dt['start'] = (dt.PY.astype(int).astype(str) + '-' + dt.month.astype(str) + '-'
+ dt.date.astype(str))
li_date_end = []
for a, b in zip(dt.PD, dt.start):
if type(a) == datetime.datetime:
li_date_end.append(a.day)
else:
li_date_end.append(pd.Period(b).days_in_month)
dt['date_end'] = li_date_end
dt['end'] = (dt.PY.astype(int).astype(str) + '-' + dt.month.astype(str) + '-'
+ dt.date_end.astype(str))
dt['start'] = dt['start'].apply(pd.to_datetime)
dt['end'] = dt['end'].apply(pd.to_datetime)
# FIXME: change from WC to SC
dt['query'] = ('(SO=(' + dt.SO + ')) AND DOP=(' + dt.start.astype(str) + '/'
+ dt.end.astype(str) + ') AND DT=(Article) AND SU=('
+ dt.SC + ')')
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()
try:
print('Wait for popup')
loc = (By.XPATH, "//button[@class='_pendo-close-guide']")
wait = WebDriverWait(browser, 30, 2)
wait.until(EC.visibility_of_element_located(loc))
print('Close guide popup')
# close popup guide
xpath = "//button[@class='_pendo-close-guide']"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
print('Click on popup')
except Exception as e:
print(e)
try:
print('Wait for cookie popup')
loc = (By.XPATH, "//button[@id='onetrust-accept-btn-handler']")
wait = WebDriverWait(browser, 30, 2)
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, 100):
print(start)
if start >= len(dt):
break
ofn = DOWNLOAD_PATH + '/' + str(start + 1) + '.xls'
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)
loc = (By.ID, "advancedSearchInputArea")
wait = WebDriverWait(browser, 5)
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")
elem.send_keys(dt['query'][start])
# click 'Search' button
xpath = "//button[contains(., ' Search ')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
if first_search:
# FIXME: it seems popup randomly
try:
print('Wait for popup')
loc = (By.XPATH, "//button[@class='_pendo-close-guide']")
wait = WebDriverWait(browser, 2, 1)
wait.until(EC.visibility_of_element_located(loc))
# click close popup
xpath = "//button[@class='_pendo-close-guide']"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
print('Click on popup')
first_search = False
except Exception as e:
print(e)
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()
loc = (By.XPATH, "//button[contains(., 'Excel')]")
wait = WebDriverWait(browser, 5)
wait.until(EC.presence_of_element_located(loc))
# choose "Excel" selection
xpath = "//button[contains(., 'Excel')]"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
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.xls'
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)
try:
txt = "< BACK TO BASIC SEARCHES"
elem = browser.find_element(By.LINK_TEXT, txt)
elem.click()
print('Wait for popup')
loc = (By.XPATH, "//button[@class='_pendo-close-guide']")
wait = WebDriverWait(browser, 3)
wait.until(EC.visibility_of_element_located(loc))
# close popup guide
xpath = "//button[@class='_pendo-close-guide']"
elem = browser.find_element(By.XPATH, xpath)
elem.click()
print('Click on popup')
except Exception as e:
print(e)
start += 1
except Exception as e:
print(e)
finally:
browser.quit()