-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckiptagger_ptt.py
136 lines (118 loc) · 4.6 KB
/
ckiptagger_ptt.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
import requests
import jieba
from bs4 import BeautifulSoup
from ckiptagger import data_utils, construct_dictionary, WS, POS, NER
class c_ptt_requests:
def __init__(self) -> None:
self.url = 'https://www.ptt.cc/bbs/Gossiping/index.html'
self.my_headers = {'cookie' : 'over18=1;'}
self.all_url = []
self.index_url = []
def f_find_index_url(self) :
r = requests.get(self.url , headers = self.my_headers)
soup = BeautifulSoup(r.text , 'html5lib')
url = soup.find(class_='btn-group btn-group-paging')
index_url_number = url.find_all('a' , href = True)
index_url_number = str(index_url_number[1]['href'])
index_url_number = index_url_number.replace('/bbs/Gossiping/index' , '').replace('.html' , '')
index_url_number = int(index_url_number) + 1
for i in range (0,20):
url_tmp = index_url_number - i
self.index_url.append('https://www.ptt.cc/bbs/Gossiping/index'+str(url_tmp)+'.html')
def f_get_all_url(self) :
for k in self.index_url :
try :
r = requests.get(k , headers = self.my_headers)
soup = BeautifulSoup(r.text , 'html5lib')
url = soup.select('.title')
for i in url:
self.all_url.append('https://www.ptt.cc'+i.find('a' , href = True)['href'])
except :
pass
def f_get_all_context(self) :
count = 0
shhh = 0
push = 0
shhh_words = []
push_words = []
for i in self.all_url:
r = requests.get(i , headers = self.my_headers)
soup = BeautifulSoup(r.text, "html5lib")
reser = soup.select('.push')
count += 1
for i in reser:
try:
if '噓' in str(i.find('span' , class_='f1 hl push-tag').get_text()) :
shhh += 1
txt = str(i.find('span' , class_='f3 push-content').get_text().replace(':','').replace(' ',''))
shhh_words.append(txt)
except:
if '推' in str(i.find('span' , class_='hl push-tag').get_text()) :
push += 1
txt = str(i.find('span' , class_='f3 push-content').get_text().replace(':','').replace(' ',''))
push_words.append(txt)
print(count)
return shhh_words , push_words
class c_ckiptagger_ptt:
def __init__(self , push_words , shhh_words) -> None:
self.push_words = push_words
self.shhh_words = shhh_words
def f_ckiptagger_ptt(self):
push_word_count = {}
shhh_word_count = {}
del_push = []
del_shhh= []
ws = WS("./data")
'''
pos = POS("./data")
ner = NER("./data")
'''
word_sentence_list = ws(
self.push_words,
)
'''
pos_sentence_list = pos(word_sentence_list)
entity_sentence_list = ner(word_sentence_list, pos_sentence_list)
print('WS: ', word_sentence_list)
print('POS: ', pos_sentence_list)
print('NER: ', entity_sentence_list)
'''
for i in word_sentence_list :
for k in i :
if k in push_word_count:
push_word_count[k] +=1
else:
push_word_count[k] = 1
for i in push_word_count :
if push_word_count[i] < 50 :
del_push.append(i)
for i in del_push :
del push_word_count[i]
print("push_words:\n")
print(sorted(push_word_count.items(), key=lambda x:x[1]))
word_sentence_list = ws(
self.shhh_words,
)
for i in word_sentence_list :
for k in i :
if k in shhh_word_count:
shhh_word_count[k] +=1
else:
shhh_word_count[k] = 1
for i in shhh_word_count :
if shhh_word_count[i] < 50 :
del_shhh.append(i)
for i in del_shhh :
del shhh_word_count[i]
print("shhh_words:\n")
print(sorted(shhh_word_count.items(), key=lambda x:x[1]))
def main():
#data_utils.download_data_gdown("./") 第一次運行才需要
ptt_requests_q = c_ptt_requests()
ptt_requests_q.f_find_index_url()
ptt_requests_q.f_get_all_url()
shhh_words , push_words = ptt_requests_q.f_get_all_context()
ptt_c = c_ckiptagger_ptt(shhh_words=shhh_words , push_words=push_words)
ptt_c.f_ckiptagger_ptt()
if __name__ == '__main__':
main()