-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
287 lines (187 loc) · 9.28 KB
/
scraping.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
import sys
import time
import datetime
from unidecode import unidecode
import pandas as pd
import numpy as np
import os
try:
from Bio import Entrez
except:
pass
try:
from scholarly import scholarly
except:
pass
try:
import requests
#import feedparser
except:
pass
class abstractScraper():
""" Basic scraper for downloading abstracts"""
def __init__(self, keywords, database='pubmed', email=None, retmax=1000, outPath='./', verbose=False):
"""
Args:
keywords (list of str): list of keywords to investigate.
database (list or str): string or list of strings of databases to scrape
'pubmed', 'biorxiv' and 'scholar' are currently implemented (default 'pubmed').
email (str): email address for PubMed retrival.
retmax (int): total number of IDs to retrieve per database.
outPath (str): path to folder where files will be saved.
verbose (bool): activate verobse option (search timing).
"""
self.email = email
self.keywords = ' '.join(keywords)
self.database = database
if not isinstance(database, list):
self.database=[self.database]
if 'pubmed' in self.database and 'Bio' not in sys.modules:
warnings.warn('Bio.Entrez not found, skipping PubMed search')
self.database=[x for x in self.database if x!='pubmed']
if 'biorxiv' in self.database and 'requests' not in sys.modules:
warnings.warn('Requests not found, skipping Biorxiv search')
self.database=[x for x in self.database if x!='biorxiv']
if 'scholar' in self.database and 'scholarly' not in sys.modules:
warnings.warn('Scholarly not found, skipping Google Scholar search')
self.database=[x for x in self.database if x!='scholar']
self.retmax = retmax
self.chunkSize = 50
self.outPath = outPath
if not os.path.exists(self.outPath):
os.makedirs(self.outPath)
self.verbose = verbose
self.found = []
def searchEntrez(self):
""" Search query on PubMed with Entrez.
Returns:
(list) list of paper IDs found by the scraper.
"""
Entrez.email = self.email
handle = Entrez.esearch(db='pubmed',
sort='relevant',
retmax=str(self.retmax),
retstart = '10',
retmode='xml',
#reldate = '3650',
term=self.keywords)
return Entrez.read(handle)['IdList']
def fetchDetails(self, idsList):
""" Fetch details for a list of publication IDs.
Args:
isdList (list of str): list of IDs.
Returns:
(list): details for each publication in input.
"""
handle = Entrez.efetch(db='pubmed',
retmode='xml',
id=','.join(idsList))
return Entrez.read(handle)
def scrapePubMed(self):
""" Download papers information from PubMed. """
ids = self.searchEntrez()
for chunkIx in range(0, len(ids), self.chunkSize):
chunk = ids[chunkIx : chunkIx + self.chunkSize]
try:
papers = self.fetchDetails(chunk)
for i, paper in enumerate(papers['PubmedArticle']):
self.found.append([unidecode(paper['MedlineCitation']['Article']['ArticleTitle']),
unidecode(paper['MedlineCitation']['Article']['Abstract']['AbstractText'][0]),
unidecode(paper['MedlineCitation']['PMID']),
unidecode(paper['MedlineCitation']['Article']['Journal']['Title']),
unidecode(paper['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Year'])])
if 'Month' in paper['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']:
self.found[-1].append(unidecode(paper['MedlineCitation']['Article']['Journal']['JournalIssue']['PubDate']['Month']))
else:
self.found[-1].append(np.nan)
auts = []
for aut in paper['MedlineCitation']['Article']['AuthorList']:
auts.append(aut['Initials']+' '+aut['LastName'])
self.found[-1].append(auts)
except:
pass
def scrapeBiorxivFeed(self):
""" DEPRECATED: download papers information from biorixiv with feeds (can only access last 30 abstracts). """
feed = feedparser.parse('http://connect.biorxiv.org/biorxiv_xml.php?subject=%s'\
% ('biochemistry+bioinformatics+biophysics+cancer_biology+cell_biology+\
clinical_trials+genetics+genomics+immunology+molecular_biology+pathology'))
for i in range(len(feed.entries)):
if any(key.lower() in feed.entries[i].summary.lower() for key in self.keywords.split()):
self.found.append([feed.entries[i].title.replace('\n', " "),
feed.entries[i].summary.replace('\n', " "),
feed.entries[i].link.replace('\n', " "),
'bioRxiv',
feed.entries[i].updated[:4],
feed.entries[i].updated[5:7],
feed.entries[i].author
])
def scrapeBiorxiv(self):
""" Download papers information from biorixiv. """
# WARNING: not a great implementation, particularly inefficient.
collection = ['','']
totalpapers = 0
chunkIx = 0
while len(collection)!=0 and chunkIx<100000:
if chunkIx > 10000 and totalpapers < chunkIx/100:
print('WARNING: too few papers found, bioRxiv search stopped.')
break
if totalpapers >= self.retmax:
break
collection = requests.get('https://api.biorxiv.org/details/biorxiv/2015-01-01/{}/{:d}'\
.format(datetime.date.today().strftime('%Y-%m-%d'),chunkIx)).json()['collection']
chunkIx += 100
for paper in collection:
#print(paper['abstract'])
if any(key.lower() in paper['abstract'].lower() for key in self.keywords.split()):
self.found.append([paper['title'].replace('\n', " "),
paper['abstract'].replace('\n', " "),
paper['doi'].replace('\n', " "),
'bioRxiv',
paper['date'][:4],
paper['date'][5:7],
paper['authors']
])
totalpapers += 1
def scrapeScholar(self):
""" Download papers information from Scholar """
for i, paper in enumerate(scholarly.search_pubs(self.keywords)):
if 'abstract' in paper.bib:
self.found.append([paper.bib['title'],
paper.bib['abstract'],
paper.bib['url'],
paper.bib['venue'],
paper.bib['year'],
'',
paper.bib['author']
])
if i == self.retmax -1:
break
def scrape(self):
""" Run a full search on the databases of choice. """
start = time.time()
""" PubMed """
if 'pubmed' in self.database:
self.scrapePubMed()
""" Biorxiv """
if 'biorxiv' in self.database:
self.scrapeBiorxiv()
""" Google Scholar """
if 'scholar' in self.database:
self.scrapeScholar()
self.found=pd.DataFrame(self.found, columns=['title','abstract','PMID/URL/DOI','journal','year','month','author']).astype(str).fillna('')
self.found=self.found.drop_duplicates(subset='title')
end = time.time()
if self.verbose:
print('Titles saved {:d}'.format(self.found.shape[0]))
print('Time elapsed: {}'.format(datetime.timedelta(seconds=round(end - start))))
def save(self, format='hdf5'):
""" Save the papers to disk.
Args:
format (str): the output file format (only hdf5 and csv are available, default hdf5).
"""
if format == 'hdf5':
self.found.to_hdf(os.path.join(self.outPath,'scraped.h5'), key='df')
elif format == 'csv':
self.found.to_csv(os.path.join(self.outPath,'scraped.csv'))
else:
print('Error: '+str(format)+' is an invalid format (only hdf5 and csv are currently available).')