-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper-td.py
87 lines (76 loc) · 2.04 KB
/
scraper-td.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
#!/usr/bin/env python
#
import sys
from BeautifulSoup import BeautifulSoup
if __name__ == "__main__":
path = sys.argv[1]
html = open(path, 'r').read()
soup = BeautifulSoup(html)
divs = soup.findAll('div', {'class': 'post'})
print 'found %d divs' % len(divs)
headline_tags = set(
["entry-title"]
)
date_tags = set(
["entry-meta"]
)
body_tags = set(
["entry-content"]
)
transcript_tags = set(
["tet-dev"]
)
# HEADLINE , BODY, TRANSCRIPT
headline = []
body = []
date = []
transcript = []
for div in divs:
classes = set()
ps = div.findAll('p')
important = []
for p in ps:
c = p.get('class')
if not c or c in ignore_tags:
continue
classes.add(c)
important.append(p)
print "-------------"
print p.text
if ('tet-dev'):
del p['class']
data = str(p)
body.append(data)
print p
else:
print important
for p in important:
c = p.get('class')
data = str(p)
if c in headline_tags:
headline.append(data)
print data
elif c in date_tags:
date.append(data)
elif c in body_tags:
body.append(data)
elif c in transcript_tags:
transcript.append(data)
print
body.append(str(p))
h = ''.join(headline)
b = ''.join(body)
d = ''.join(date)
t = ''.join(transcript)
fields = [h, b, t]
# do some error checking!!
for f in fields:
if '\t' in f:
raise Exception("illegal tab character found!!")
elif '\t' in f:
raise Exception('illegal newline character found!!')
#for i in range(0, len(fields)):
# print i
# print "---------"
# print fields[i]
print '\n'.join(fields)