-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkextract.py
223 lines (180 loc) · 5.98 KB
/
bkextract.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
# -*- coding: utf-8 -*-
from BeautifulSoup import BeautifulSoup
import re
import datetime
import pymongo
mapping = {
u">" : u">",
u"<" : u"<",
u"&": u"&",
u""":u'"',
u"“":u'“',
u"”":u'”',
u"‘":u"‘",
u"’":u"’"
}
def convert_special_html(mapping, text):
for old in mapping:
new = mapping[old]
text = text.replace(old, new)
return text
def getGender(text):
try:
gender_cht = text.split(u'性別')[1].replace(':', '').strip().split(u"感謝")[0]
except:
return 'unknown'
if u'女' in gender_cht:
gender = 'F'
elif u'男' in gender_cht:
gender = 'M'
else:
gender = 'unknown'
return gender
def getPostTextNum(text):
"""
text: u"客棧之光 文章:2,073性別: 女生感謝: 372次/338篇註冊日期: 2006-08-19"
"""
res = re.findall(u"文章"r":\s?([0-9,?]+)", text, re.IGNORECASE)
res = [int(x.replace(',','')) for x in res]
if res:
return res[0]
else:
return None
# postNum = re.findall(r"(?i):[0-9],[0-9]{3}",text)[0]
def getPostOfThanksNum(text):
# 感謝: 3次/2篇
# thanksNum_cht = text.split(u'')[1].replace(':', '').strip()
res = re.findall(u"感謝"r":\s*([0-9,]+)"u"次"r"/([0-9,]+)"u"篇",text)
if res:
thanksNum = tuple( map(lambda x: int(x.replace(',', '')), res[0]) )
else:
thanksNum = (None, None)
return thanksNum
def getTitleDate(text):
# e.g., icon and title5月份開始馬德里機場進出補3歐 -status icon and datestatus icon and date2012-05-23, 07:41/ status icon and date
title = text.split('-status')[0].split('icon and title')[-1].strip()
## get 2012-05-23, 07:41
## date_str_tpl: ('2012', '05', '23', '07', '41')
date_str_tpl = re.findall(r'([0-9]{4})-([0-9]{2})-([0-9]{2}),\s([0-9]{2}):([0-9]{2})', text)[0]
## date_tpl: (2012, 5, 23, 7, 41)
date_tpl = tuple(map(lambda x:int(x), date_str_tpl))
## this yields a datetime object, e.g., datetime.datetime(2012, 5, 23, 7, 41)
# datetime.datetime(date_tpl[0], date_tpl[1], ...)
date = datetime.datetime(*date_tpl)
return (title, date)
def content_filter(text):
## drop html tags
text = re.sub(r'(?i)</?[a-z]+>', '', text)
## drop urls such as "http://www.backpackers.com.tw/forum/showthread.php?p=3995072"
text = re.sub(r'(?i)http[s]?://[a-z0-9./?=&]+', '', text)
## trim spaces
text = text.strip()
return text
def extract_post_content(post):
"""
Parameters
post: a <BeautifulSoup.Tag> object
Returns:
extracted data in dictionary format
"""
## get current post id
res = re.findall(r'^edit([0-9]+)$', post.attrMap['id'])
if res:
post_id = res[0]
else:
## no id found: skip this post
## return empty dictionary
return False
## get username
username = post.find(attrs={"class": "bigusername"}).text
## get post content
content_raw = post.find(id='post_message_'+post_id)
## remove quotation
content =[]
for post_content in content_raw:
## check if current content is a quote or not
isQuote = False
try:
quote = post_content.find(attrs={'class':'alt2 inner-quote'})
isQuote = True if quote else False
except TypeError:
isQuote = False
## do nothing if a quote shows up
if isQuote:
continue
else:
## NavigableString --> str(...)
text = ""
try:
text = post_content.text
except AttributeError:
text = unicode(post_content)
text = text.strip()
if not text:
continue
# print 'post_content:', [text]
## chang > to >...etc
pruned = convert_special_html(mapping, text=text)
## eliminate urls, </div>.
cleancontent = content_filter(pruned)
# print cleancontent
content.append(cleancontent)
## get gender, title and date
date, gender, title = None, None, None
# print '>>>> post'
for x in post.findAll(attrs={"class": "smallfont"}):
# print x.text
# print u'性別' in x.text
# print u'文章' in x.text
# print u'感謝' in x.text
# print '<<'
# raw_input()
## get gender
if u'性別' in x.text:
gender = getGender(x.text)
## get No.post
if u"文章" in x.text:
post_frequency = getPostTextNum(x.text)
## get No.be thanked / posts
if u'感謝' in x.text:
post_of_thanks_number= getPostOfThanksNum(x.text)
## get title and date
if x.text.startswith('icon and title'):
title, date = getTitleDate(x.text)
data = {
'username': username,
'content': content,
#'content_raw': content_raw,
'date': date,
'title': title,
'gender': gender,
## post_frequency: int or None
'No._of_post': post_frequency,
## post_of_thanks_number: tuple ( <int>次, <int>篇)
'thanks(times,articles)': post_of_thanks_number
}
return data
def display(data):
for k, v in data.items():
print k, ':', v
if __name__ == '__main__':
## connect to mongodb
db = pymongo.Connection('doraemon.iis.sinica.edu.tw')['espanol']
## use certain collection
co = db['bk.posts']
src = "all_bkcont_test/10.html"
## load html file
raw_html = open(src).read()
soup = BeautifulSoup(raw_html)
## find all posts
posts = soup.findAll(id=re.compile(r"^edit[0-9]+$"))
for post in posts:
data = extract_post_content(post)
if not data:
## not a valid post. Ad perhaps
continue
data['source'] = src
display(data)
raw_input()
## uncomment this line to performa mongo insertion
# co.insert(data)