-
Notifications
You must be signed in to change notification settings - Fork 12
/
news_crawler.py
396 lines (365 loc) · 16.3 KB
/
news_crawler.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import requests, glob, datetime,re,time
from bs4 import BeautifulSoup
from urllib.parse import urlparse
'''
This file is used for web crawling,
the output rows are eventid, date, title, abstract, content
'''
hostlist = ['www.abc.net.au', 'www.news.com.au', 'au.news.yahoo.com','www.smh.com.au','www.dailytelegraph.com.au',
'www.theage.com.au', 'www.theguardian.com', 'www.brisbanetimes.com.au', 'www.9news.com.au',
'www.afr.com', 'www.heraldsun.com.au', 'www.couriermail.com.au']
# parse rule for www.abc.net.au
def host1(soup):
string = ''
#print(soup.prettify())
h1Tag = soup.find('h1', itemprop='headline')
if h1Tag is not None:
#print('1')
abstract = stringfy(soup.find('div', class_='tg-tlc-storybody_intro').contents[0])
string = string + stringfy(h1Tag.contents[0]) + '\t' + abstract + '\t'
ptags = soup.find('div', class_='tg-tlc-storybody').find_all('p')
for i in range(0, len(ptags) - 1):
tag = ptags[i]
if not tag.has_attr('class'):
if i == 1:
string = string + stringfy(tag.contents[0]).replace('\n', '') + '\t'
else:
if tag.contents[0] != '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
#print(string)
else:
#print('2')
divTag = soup.find("div", class_='article section')
if divTag is not None:
h1Tag = divTag.find('h1')
abstract = ''
if soup.find('div', class_='tg-tlc-storybody') is not None:
ptags = soup.find('div', class_='tg-tlc-storybody').find_all('p')
for i in range(0, len(ptags)):
tag = ptags[i]
if not tag.has_attr('class'):
if tag.contents[0] != '':
string = string + stringfy(tag.contents[0]) + ' '
elif tag['class'][0] == 'first':
abstract = stringfy(tag.contents[0])
string = stringfy(h1Tag.contents[0]) + '\t' + abstract + '\t' + string
else:
#print('3')
ptags = soup.find('div', class_='article section').find_all('p')
abstract = stringfy(soup.find('meta', property='og:description')['content'])
for tag in ptags:
if not tag.has_attr('class'):
if tag.contents[0] != '':
string = string + stringfy(tag.contents[0]) + ' '
string = stringfy(h1Tag.contents[0]) + '\t' + abstract + '\t' + string
else:
#print('4')
h1Tag = soup.find('h1', itemprop='name')
if h1Tag is not None:
title = stringfy(h1Tag.contents[0])
abstract = title
comp_texts = soup.find_all('div', class_='comp-rich-text')
for item in comp_texts:
ptags = item.find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
if tag.contents[0] != '':
string = string + remove_tag(stringfy(tag.contents[0])) + ' '
string = title + '\t' + abstract + '\t' + string
else:
contentTag = soup.find('div', class_='content')
if contentTag is not None:
title = stringfy(contentTag.find('h1').contents[0])
abstract = stringfy(soup.find('meta', attrs={"name": "description"})['content'])
ptags = soup.find('div', id='content').find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
if tag.contents[0] != '':
string = string + stringfy(tag.contents[0]) + ' '
elif tag['class'] == 'first':
string = stringfy(tag.contents[0]) + ' ' + string
string = title + '\t' + abstract + '\t' + string
else:
contentTag = soup.find('div', id='main')
if contentTag is not None:
title = stringfy(contentTag.find('h1').contents[0])
abstract = title
p = soup.find('div', id='article')
if p is not None:
paragraph = ''
for content in p.contents:
paragraph = paragraph.strip() + ' ' + remove_tag(
stringfy(content).replace('<br/>', '')).replace(
'Do you have a comment or a story idea? Get in touch with the Lateline team by clicking here.',
'')
string = title + '\t' + abstract + '\t' + paragraph
else:
p = soup.find('div',class_='story')
abstract = stringfy(remove_tag(p.find('div',class_='summary').find('p').contents[0]))
ptags = p.find('div',class_='story_body').find_all('p')
for tag in ptags:
if tag.contents[0] != '':
string = string + remove_tag(stringfy(tag.contents[0])) + ' '
string = title + '\t' + abstract + '\t' + string
else:
print('fail')
return string
# parse www.news.com.au
def host2(soup):
#print(soup.prettify())
string = ''
h1Tag = soup.find('h1', class_='story-headline')
if h1Tag is not None:
title = stringfy(h1Tag.contents[0])
abstract = stringfy(soup.find('p', class_='intro').contents[0])
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='story-content').find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]) + ' '
#print(string)
return string
else:
h1Tag = soup.find('h1', itemprop= 'headline')
if h1Tag is not None:
title = stringfy(h1Tag.contents[0])
abstract = stringfy(soup.find('div', class_='tg-tlc-storybody_intro').contents[0])
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='tg-tlc-storybody').find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]) + ' '
#print(string)
return string
# parse au.news.yahoo.com
def host3(soup):
string = ''
title = stringfy(soup.find('h1', class_='headline').contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='article-container').find_all('p')
# for tag in ptags:
for tag in ptags:
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
return string
# parse www.smh.com.au
def host4(soup):
string = ''
title = stringfy(soup.find('header', class_='article__header').find('h1').contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='article__body').find_all('p')
for tag in ptags:
if ptags.index(tag) == len(ptags) - 1:
break
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
return string
# parse www.dailytelegraph.com.au
def host5(soup):
string = ''
title = str(soup.find('h1', itemprop='headline').contents[0]).replace('\n', '').strip()
abstract = str(soup.find('div', class_='tg-tlc-storybody_intro').contents[0]).replace('\n', '').strip()
abstract = remove_tag(abstract)
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='tg-tlc-storybody').find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
paragraph = str(tag.contents[0])
if not paragraph == '':
string = string + str(tag.contents[0]).replace('\n', '') + ' '
string = re.sub('<[^>]*>', '', string).strip()
return string
# parse www.theage.com.au
def host6(soup):
string = ''
title = str(soup.find('h1').contents[0]).replace('\n', '').strip()
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if not tag.has_attr('class'):
paragraph = str(tag.contents[0])
if not paragraph == '':
string = string + str(tag.contents[0]).replace('\n', '') + ' '
return string
# parse www.theguardian.com
def host7(soup):
string = ''
title = stringfy(soup.find('h1', class_='content__headline').contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='content__article-body').find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
return string
# parse www.brisbanetimes.com.au
def host8(soup):
string = ''
title = stringfy(soup.find('h1').contents[0])
abstract = title
# print(title)
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('article').find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if len(tag.contents) > 1:
if tag.has_attr('data-reactid'):
for content in tag.contents:
if 'react-text' not in content and content is not None:
tmp = stringfy(content)
content = remove_tag(tmp)
paragraph = stringfy(content)
if not paragraph == '':
string = string + stringfy(paragraph) + ' '
string = string.replace("By signing up you accept our privacy policy and conditions of use", "").strip()
return string
# parse www.9news.com.au
def host9(soup):
string = ''
title = stringfy(soup.find('h1', class_='article__headline').contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='article__body-croppable').find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
string = string.replace("© Nine Digital Pty Ltd 2017", "").strip()
return string
# parse www.afr.com
def host10(soup):
string = ''
title = stringfy(soup.find('h1', itemprop="headline name").contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='article__content').find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if not tag.has_attr('class'):
tmp = stringfy(tag.contents[0])
content = remove_tag(tmp)
paragraph = stringfy(content)
if not paragraph == '':
string = string + content.replace('\n', '') + ' '
return string
# parse www.heraldsun.com.au
def host11(soup):
string = ''
title = stringfy(soup.find('h1', class_="tg-tlc-storyheader_titlewrapper_h1").contents[0])
abstract = stringfy(soup.find('div', class_='tg-tlc-storybody_intro').find('p').contents[0])
string = string + title + '\t' + abstract + '\t'
ptags = soup.find('div', class_='w_tg-tlc-storybody').find_all('p')
for tag in ptags:
# if ptags.index(tag) == len(ptags)-1:
# break
if not tag.has_attr('class'):
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
return string
# parse www.couriermail.com.au
def host12(soup):
string = ''
title = stringfy(soup.find('h1').contents[0])
abstract = title
string = string + title + '\t' + abstract + '\t'
ptags = soup.find_all('p')
for tag in ptags:
if not tag.has_attr('class'):
paragraph = ''
if len(tag.contents) > 0:
paragraph = stringfy(tag.contents[0])
if not paragraph == '':
string = string + stringfy(tag.contents[0]).replace('\n', '') + ' '
return string
def write_to_file(string,path):
with open(path,'a',encoding='utf-8') as f:
f.write(string+'\n')
def write_to_log(string):
with open('dm_crawling.log','a',encoding='utf-8') as f:
f.write(string+' '+ stringfy(datetime.datetime.now()) +'\n')
def stringfy(c):
return str(c).replace('\n', '').strip()
def remove_tag(c):
return re.sub('<[^>]*>', '', c)
# main running part
def main(year):
filelist = glob.glob('url_201304now/'+stringfy(year)+'/*.CSV')
for f in filelist:
filename = f.split('/')[-1]
write_path = 'news_201304/'+stringfy(year)+'/' + filename
with open(f,'r') as file:
for line in file:
data = line.split('\t')
hostname = urlparse(data[5]).hostname
# if hostname in hostlist:
if hostname in hostlist:
url = data[6]
url = url.replace('\n','')
eventid = data[0]
eventdate = data[1]
try:
r = requests.get(url)
#print(stringfy(r.url))
if r.status_code == 200:
string = ''
c = r.content
soup = BeautifulSoup(c, 'lxml')
index = hostlist.index(hostname)
if index == 0:
# print(url)
string = host1(soup)
# print(string)
elif index == 1:
#print(url)
string = host2(soup)
elif index == 2:
string = host3(soup)
elif index == 3:
string = host4(soup)
elif index == 4:
string = host5(soup)
elif index == 5:
string = host6(soup)
elif index == 6:
string = host7(soup)
elif index == 7:
string = host8(soup)
elif index == 8:
string = host9(soup)
elif index == 9:
string = host10(soup)
elif index == 10:
string = host11(soup)
elif index == 11:
string = host12(soup)
# finalize the string
string = eventid+'\t'+eventdate+'\t'+string
write_to_file(string,write_path)
write_to_log(eventid)
except Exception as e:
write_to_log(stringfy(e)+' '+url)
print(url)
print(e)