-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_journal_parser.py
275 lines (199 loc) · 8.51 KB
/
new_journal_parser.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
import sys
import json
import os
import os.path
import shutil
import time
from multiprocessing import Process, Queue
import requests
from bs4 import BeautifulSoup
def define_soup(url):
"""Defines soup object for given url and returns it"""
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
response = requests.get(url, headers=header)
soup = BeautifulSoup(response.text, 'html.parser')
return soup
def extract_volume_links(request_link, path):
"""Extracts links to all volumes from response json"""
domain = 'http://www.sciencedirect.com'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
response = requests.get(request_link, headers=header)
json_string = response.text
data = json.loads(json_string)
data = data.get('data', {})
results = {}
for obj in data:
volume_name = obj.get('volIssueSupplementText', '')
link_to_volume = '{}/journal/{}{}'.format(domain, path, obj.get('uriLookup', ''))
results.update({volume_name: link_to_volume})
return results
def get_all_volumes(json_string):
"""Gets all volume links"""
domain = 'http://www.sciencedirect.com'
json_string = json.loads(json_string) # I don't know why, but loads() returns str...
json_data = json.loads(json_string) # so that's why I'm doing this again
path = json_data.get('titleMetadata', {}).get('title') # journal url path-name
data = json_data.get('issuesArchive', {}).get('data', {}).get('results', {})
results = {}
if not data:
return results
journal_id = data[0].get('firstIssue', {}).get('issn')
for obj in data:
year = str(obj.get('year', ''))
request_link = '{}/journal/{}/year/{}/issues'.format(domain, journal_id, year)
to_update = extract_volume_links(request_link, path)
results.update(to_update)
return results
def goto_all_issues(url):
"""Returns url to all issues from main journal page"""
soup = define_soup(url)
tag = soup.find('a', {'class': 'js-latest-issues-link-text'})
domain = 'http://www.sciencedirect.com'
link = tag.get('href')
link_to_all_volumes = '{}{}'.format(domain, link)
return link_to_all_volumes
def extract_json(url, is_soup=False):
"""
Extracts json string from given url.
If is_soup=True - extracts json string from given soup.
"""
json_string = ''
if is_soup:
soup = url
else:
soup = define_soup(url)
tag = soup.find('script', {'type': 'application/json'})
if tag:
json_string = tag.string
return json_string
def get_journal_name(soup):
"""Gets journal name from given soup"""
journal_name = soup.find('input', {'name': 'pub'}).get('value')
return journal_name
def collect_urls_to_parse(urls, destination_queue):
"""Collects all urls to parse"""
for url in urls:
link_to_all_volumes = goto_all_issues(url) # url to page that contain all issues
soup = define_soup(link_to_all_volumes)
journal_name = get_journal_name(soup).replace(':', '-')
json_string = extract_json(soup, is_soup=True)
volumes = get_all_volumes(json_string) # gets urls to all volumes in journal
destination_queue.put({
journal_name: volumes
})
def except_get_data(data):
"""Gets data for exception case"""
for obj in data: # complex json structure
issue_sec = obj.get('issueSec', {})
for issue in issue_sec:
items = issue.get('includeItem', {})
for item in items:
authors = item.get('authors', {})
for author in authors:
name = author.get('givenName')
surname = author.get('surname')
author_name = '{} {}'.format(name, surname)
emails = author.get('emails')
if emails:
yield {author_name: emails}
def alternative_get_data(data):
"""Tries alternative case to extract needed data"""
for obj in data: # complex json data structure
if 'issueSec' in obj:
yield except_get_data(data)
return
items = obj.get('includeItem', {}) # or obj.get('issueSec', {})
for item in items:
authors = item.get('authors')
for author in authors:
name = author.get('givenName')
surname = author.get('surname')
author_name = '{} {}'.format(name, surname)
emails = author.get('emails')
if emails:
yield {author_name: emails}
def parse_journal(url):
"""Parses given volume of journal"""
json_string = extract_json(url)
json_string = json.loads(json_string)
json_data = json.loads(json_string)
data = json_data.get('articles', {}).get('ihp', {}).get('data', {}).get('issueBody', {}).get('includeItem', [])
if not data:
data = json_data.get('articles', {}).get('ihp', {}).get('data', {}).get('issueBody', {}).get('issueSec', {})
yield alternative_get_data(data)
return
for obj in data: # complex json data structure
authors = obj.get('authors')
for obj2 in authors:
name = obj2.get('givenName')
surname = obj2.get('surname')
author = '{} {}'.format(name, surname)
emails = obj2.get('emails')
if emails:
yield {author: emails}
def write_results(source_queue):
"""Write results in file"""
prev_journal = None
current_file = None
try:
while True:
data = source_queue.get()
filename = '{}.txt'.format(data.get('journal_name'), 'not_found')
authors = data.get('author', {})
if prev_journal != filename:
if current_file:
current_file.close()
shutil.move(
os.path.realpath(prev_journal),
os.path.realpath('Results/{}'.format(prev_journal))
) # move result file in "Results" directory
current_file = open(filename, 'w', encoding='utf-8')
[(author, emails)] = authors.items()
emails = ', '.join(emails)
to_write = '{}; {}\n'.format(author, emails) # writes csv-like file (.txt) to further import to excel
current_file.write(to_write)
prev_journal = filename
except KeyboardInterrupt:
current_file.close()
shutil.move(
os.path.realpath(prev_journal),
os.path.realpath('Results/{}'.format(prev_journal))
)
def parse_urls(source_queue, destination_queue):
while True:
[(journal_name, volumes)] = source_queue.get().items()
print('Parsing "{}"'.format(journal_name))
for volume_name, url in volumes.items():
print('\t{}'.format(volume_name))
for author in parse_journal(url):
destination_queue.put({
'journal_name': journal_name,
'author': author
})
def main():
urls_from_file = sys.stdin.read().split() # takes input
urls_from_file = [url.strip() for url in urls_from_file] # formatizing input
if not os.path.exists('Results'): # directory to save results files with authors and emails
os.makedirs('Results')
urls_to_parse_queue = Queue()
data_to_write_queue = Queue()
collect_urls_process = Process(target=collect_urls_to_parse, args=(urls_from_file, urls_to_parse_queue))
parse_urls_process = Process(target=parse_urls, args=(urls_to_parse_queue, data_to_write_queue))
write_to_file_process = Process(target=write_results, args=(data_to_write_queue,))
print('Launching processes')
collect_urls_process.start()
parse_urls_process.start()
write_to_file_process.start()
collect_urls_process.join()
parse_urls_process.join()
write_to_file_process.join()
if __name__ == '__main__':
t = time.time()
print('Parsing new journals...')
main()
print('Done. Check nearby folder "Results" to see result.')
print(time.time() - t)