-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_cell.py
190 lines (176 loc) · 6.33 KB
/
scrape_cell.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
import sys
import argparse
if __name__ == "__main__":
import re
import requests
import bs4
parser = argparse.ArgumentParser()
parser.add_argument("--index_url", help="e.g. https://www.cell.com/cell-metabolism/fulltext/S1550-4131(18)30735-6", type=str, default="https://www.cell.com/cell-metabolism/fulltext/S1550-4131(18)30735-6")
parser.add_argument("--play",action='store_true',help="specifies for the audio to be played immediately- default is to not play")
parser.add_argument('--output_file',type=str,help="if not specified- will default to title of the paper")
parser.add_argument('--speech_rate',type=int,help="default is 220 wpm",default=220)
parser.add_argument('--voice',type=str,help='Choose any from: Alex\nalice\nalva\namelie\nanna\ncarmit\ndamayanti\ndaniel\ndiego\nellen\nfiona\nFred\nioana\njoana\njorge\njuan\nkanya\nkaren\nkyoko\nlaura\nlekha\nluca\nluciana\nmaged\nmariska\nmei-jia\nmelina\nmilena\nmoira.premium\nmonica\nnora\npaulina\nsamantha\nsara\nsatu\nsin-ji\ntessa\nthomas\nting-ting\nveena\nVictoria\nxander\nyelda\nyuna\nyuri\nzosia\nzuzana\n',default='Alex')
args = parser.parse_args()
print('voice '+args.voice)
response = requests.get(args.index_url)
soup = bs4.BeautifulSoup(response.text,'html.parser')
mystring = soup.find_all('div', attrs={'class': 'col-md-7 col-lg-9 article__sections'})[0].text
#extra_string=soup.find_all('section', {'id': re.compile(r'sec')})
# full=''
# for i in extra_string:
# full=full+i.text.encode('ascii','ignore')
#mystring=mystring+extra_string
header=soup.select('h1')[0].text.encode('utf8')
def a(test_str):
ret = ''
skip1c = 0
skip2c = 0
for i in test_str:
if i == '[':
skip1c += 1
elif i == ']' and skip1c > 0:
skip1c -= 1
elif skip1c == 0:
ret += i
return ret
# def a2(test_str):
# regex=re.compile('\\nGoogle Scholar\)')
# ends=[]
# diff=0
# difs=[]
# for m in regex.finditer(test_str):
# ends.append(m.end())
# for m in ends:
# difs.append(diff)
# m=m-sum(difs)
# i=m
# while True:
# if bool(re.match('\(\D+',test_str[i-2:i])):
# #if 'et al' in test_str[i-2:i+5]:
# test_str=test_str[:i-2]+test_str[m:]
# diff=m-i
# break
# else:
# i-=1
# return test_str
# def a2(test_str):
# regex=re.compile('\\nGoogle Scholar\)')
# starts=[]
# ends=[]
# diff=0
# ret=''
# difs=[]
# for m in regex.finditer(test_str):
# ends.append(m.end())
# for m in ends:
# difs.append(diff)
# m=m-sum(difs)
# i=m
# while True:
# if bool(re.match('\(\D+',test_str[i-2:i])):
# #if 'et al' in test_str[i-2:i+5]:
# starts.append(i-2)
# break
# else:
# i-=1
# start=test_str[:starts[0]]
# starts=starts[1:]
# end=test_str[ends[-1]:]
# ends=ends[:-1]
# for i,j in zip(starts,ends):
# ret=ret+test_str[j:i]
# return start+ret+end
x = a(mystring)
x=re.sub(r'(?s)\(.*?Google Scholar\)','',x)
#x=mystring
#x=re.sub(r'([a-z])- ([a-z])', r'\1\2', x)
#x= file.read()
#x = re.sub(r'[-\d,]+', '', x)
#x =re.sub(r'([^ 0-9])(\d+(?:,\d+)*)', r'\1', x)
#x=re.sub(r'\w*\d\w*', '', x).strip()
# x1 = re.sub('[A-Z][0-100]+', '', x)
# x2 = re.sub('[A-Z][0-100]+', '', x1)
# x3 = re.sub('[A-Z][0-100]+', '', x2)
#x = re.sub(r' ,', '', x)
x = re.sub(u'\u03b1', 'alpha', x)
x = re.sub(u'\u03b2', 'beta', x)
x = re.sub(u'\u025b', 'epsilon', x)
x = re.sub(u'\u2022', '\n\n', x)
x = re.sub(r'\xd7', 'by', x)
x = re.sub(r'\xa0', ' ', x)
x = re.sub(u'\u2212', ' to the minus ', x)
x = re.sub(u'\u2013', ' to ', x)
x = re.sub(u'\u223c', ' approximately ', x)
x = re.sub(u'\u201c', '', x)
x = re.sub(u'\u201d', '', x)
x = re.sub(u'\u2014', '-', x)
pattern="View Large\n Image\n Figure ViewerDownload Hi-res\n image\n Download (PPT)"
x=x.replace(pattern,'')
pattern="View Large\n Image\n Figure ViewerDownload Hi-res\n image\n Download"
x=x.replace(pattern,'')
Neandertal="Neandertal"
x=x.replace(Neandertal,'Neeanderthaal')
# x = re.sub(r'\xce\x94', ' delta ', x)
# x = re.sub(r'\xce\xb2', ' beta ', x)
# x = re.sub(r'\xc3\x85', ' delta ', x)
# x = re.sub(r'\xce\xb7', ' eta ', x)
# x = re.sub(r'\xcf\x83', ' sigma ', x)
# x = re.sub(r'\xcb\x86', '', x)
# x = re.sub(r'\xce\xb1', ' angstrom', x)
# x = re.sub(r'\xce\xb6', ' zeta ', x)
# x = re.sub(r'\xce\xb5', ' epsilon ', x)
# x = re.sub(r'\xc2\xb1', ' plus or minus ', x)
# x = re.sub(r'\xce\xa9', ' omega', x)
# x = re.sub(r'\xce\xbb', ' lambda ', x)
# x = re.sub(r'\xce\xb5', ' epsilon ', x)
# x = re.sub(r'\xe2\x88\x88', 'epsilon', x)
# x = re.sub(r'\xe2\x88\x91', 'sum of ', x)
# x = re.sub(r'\xce\xba', 'kappa ', x)
x=re.sub(r'\w+.jpg\s?','',x)
x=re.sub(r'et al\.','et al',x)
x=re.sub(r'\xe2\x88\xbc','approximately',x)
x=re.sub(r'3D', 'three D',x)
x=re.sub(r'\d+;', '',x)
x=re.sub(r'\d+ ;', '',x)
#x=re.sub(r'\d+,', '',x)
#x=re.sub(r'\d+\.', '.',x)
#x = re.sub(r'\r\d+', '', x)
x = re.sub(r'\d+\n', '', x)
x=re.sub(r'CpG', 'CPG',x)
x=re.sub(r'\xe2\x88\x97','',x)
if 'To read this article in full you will need to make a payment' in x:
x=x[:x.find('To read this article in full you will need to make a payment')]
else:
x=x[:x.find('.Acknowledgments')]
#print(x.encode('utf8'))
if args.output_file:
file=open(args.output_file,'wb')
file.write(x.encode('utf8'))
file.close()
else:
file=open(header+b'.txt','wb')
file.write(x.encode('utf8'))
file.close()
if args.play:
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', args.speech_rate)
voices = engine.getProperty('voices')
for j,i in enumerate(voices):
if args.voice in str(i.id):
break
engine.setProperty('voice', voices[j].id)
engine.say(x.encode('utf8'))
engine.runAndWait()
engine.stop()
# def strip_tags(html, invalid_tags):
# soup = bs4.BeautifulSoup(html)
# for tag in soup.findAll(True):
# if tag.name in invalid_tags:
# s = ""
# for c in tag.contents:
# if not isinstance(c, NavigableString):
# c = strip_tags(unicode(c), invalid_tags)
# s += unicode(c)
# tag.replaceWith(s)
# return soup