-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaver_news_word_cloud.py
367 lines (302 loc) ยท 13.3 KB
/
naver_news_word_cloud.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
#ํฌ๋กค๋ง์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
from bs4 import BeautifulSoup
import requests
import re
import datetime
import pandas as pd
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
from collections import Counter
from konlpy.tag import Okt
from PIL import Image
import numpy as np
import csv
import sys
print("python 3.10 ์์๋ง ์ฌ์ฉ๊ฐ๋ฅ, jpype ๋๋ฌธ")
# wordcloud ๋ณ๊ฒฝ
def wordcloud():
print("์๋์ฐ10/11 ์ ๊ฒฝ์ฐ ํด๋น ํ์ผ์ ๋ง์ฐ์ค ์ฌ๋ฆฌ๊ณ ์ค๋ฅธ์ชฝ ํด๋ฆญ - ๊ฒฝ๋ก๋ก ๋ณต์ฌ ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅ")
location = input("csv ํ์ผ ์์น๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ .csv ๊น์ง ์
๋ ฅ: ")
try:
location = location.replace('"', "")
except:
pass
f = open(location, 'r', encoding='utf-8')
rdr = csv.reader(f)
filter = []
print("๋ฐ์ดํฐ ์ฝ์ด์ค๋ ์ค...")
for line in rdr:
filter.append(line[3])
okt = Okt()
print("๋ช
์ฌ ์ถ์ถ ์ค...")
nouns = okt.nouns(str(filter)) # ๋ช
์ฌ๋ง ์ถ์ถ
words = [n for n in nouns if len(n) > 1] # ๋จ์ด์ ๊ธธ์ด๊ฐ 1๊ฐ์ธ ๊ฒ์ ์ ์ธ
data = Counter(words) # ์์์ ์ป์ words๋ฅผ ์ฒ๋ฆฌํ์ฌ ๋จ์ด๋ณ ๋น๋์ ํํ์ ๋์
๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๊ตฌํจ
yes_or_no = input("๋ณ๋๋ก ์ค์ ํ ๊ธ๊ผด์ด ์์ผ์ ๊ฐ์? ๊ธฐ๋ณธ์ ๋ง์ ๊ณ ๋์
๋๋ค. yes/no")
if yes_or_no == "yes":
print("์๋์ฐ10/11 ์ ๊ฒฝ์ฐ ํด๋น ํ์ผ์ ๋ง์ฐ์ค ์ฌ๋ฆฌ๊ณ ์ค๋ฅธ์ชฝ ํด๋ฆญ - ๊ฒฝ๋ก๋ก ๋ณต์ฌ ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅ")
font = input("๊ฐ์ง๊ณ ์๋ ๊ธ๊ผด ํ์ผ ๊ฒฝ๋ก ์
๋ ฅ(.ttf ๊น์ง): ")
try:
font = font.replace('"', "")
except:
pass
else:
font = 'malgun'
yes_or_no = input("๋ณ๋๋ก ์ค์ ํ ๋ฐฐ๊ฒฝ์์ด ์์ผ์ ๊ฐ์? ๊ธฐ๋ณธ์ white ์
๋๋ค 1. black 2. white ์ซ์๋ง ์
๋ ฅ")
if yes_or_no == "1":
background = 'black'
else:
background = 'white'
yes_or_no = input("๋ณ๋๋ก ์ค์ ํ ๊ฐ๋ก ์ธ๋ก๊ฐ์ด ์๋์? ๊ธฐ๋ณธ์ ๊ฐ๋ก*์ธ๋ก 1080px*1080px ์
๋๋ค yes/no")
if yes_or_no == "yes":
while yes_or_no == "yes":
try:
print("๊ฐ๋ก๊ฐ: ")
width = int(input())
print("์ธ๋ก๊ฐ: ")
height = int(input())
break
except:
print("์ซ์๋ง ์
๋ ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์")
pass
else:
width = 1080
height = 1080
yes_or_no = input("๋ณ๋๋ก ์ค์ ํ ์ต๋ ๋จ์ด ์๊ฐ ์๋์? ๊ธฐ๋ณธ์ 100๊ฐ ์
๋๋ค yes/no")
if yes_or_no == "yes":
print("๋ฝ์ ๋จ์ด ์ ์ง์ : ")
pre_max_words = input()
max_words = input(pre_max_words)
else:
max_words = int(100)
yes_or_no_2 = input("์ง์ ํ ๋ง์คํฌ๊ฐ ์๋์? yes/no")
if yes_or_no_2 == "yes":
error = int(0)
while error == int(0):
print("์๋์ฐ10/11 ์ ๊ฒฝ์ฐ ํด๋น ํ์ผ์ ๋ง์ฐ์ค ์ฌ๋ฆฌ๊ณ ์ค๋ฅธ์ชฝ ํด๋ฆญ - ๊ฒฝ๋ก๋ก ๋ณต์ฌ ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅ, .png๊น์ง ์
๋ ฅ")
icon_path = input("๊ฒฝ๋ก ์
๋ ฅ: ")
try:
icon_path = icon_path.replace('"', '')
except:
pass
try:
icon = Image.open(icon_path)
plt.imshow(icon)
global mask
mask = Image.new("RGB", icon.size, (255, 255, 255))
mask.paste(icon, icon)
mask = np.array(mask)
break
except:
print ("์๋ฌ:bad transparency mask. ๋ฐฐ๊ฒฝ์ด ํฌ๋ช
์ธ png ํ์ผ์ด์ด์ผ ํฉ๋๋ค")
print("๋ค์ ์๋ํ๋ ค๋ฉด 0, ๋ง์คํฌ ์์ด ์งํํ๋ ค๋ฉด 1์ ๋๋ฌ์ฃผ์ธ์: ")
q_error = input()
if q_error == "1":
mask = None
break
else:
pass
else:
mask = None
pass
wc = WordCloud(font_path = font, width=width, height=height, scale=2.0, max_font_size=250, max_words = max_words, background_color=background, mask=mask)
gen = wc.generate_from_frequencies(data)
plt.figure()
if yes_or_no_2 == "yes":
yes_or_no = input("๋ง์คํฌ๊ฐ ์ง์ ๋์ด ์์ต๋๋ค. ์ง์ ๋ ๋ง์คํฌ์ ์์ ๊ธ์จ์ ์
ํ์๊ฒ ์ต๋๊น? ๊ธฐ๋ณธ๊ฐ no. yes/no ์
๋ ฅ")
if yes_or_no =="yes":
try:
image_colors = ImageColorGenerator(mask)
plt.imshow(wc.recolor(color_func=image_colors))
except:
print("๋ง์คํฌ๊ฐ ์ ์์ ์ผ๋ก ๋ฑ๋ก๋์ง ์์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์งํํฉ๋๋ค")
plt.imshow(gen)
else:
plt.imshow(gen)
else:
plt.imshow(gen)
print("์ฌ์ง ํ์ผ ๋ณํ ์ค...")
now_2 = datetime.datetime.now()
now_2.strftime('%Y%m%d_%H์%M๋ถ%S์ด')
now_3 = str(now_2).replace(":", "_")
wc.to_file('์๋ํด๋ผ์ฐ๋_' + str(now_3) + '.png')
print("๋ณํ๋์์ต๋๋ค\n")
yes_or_no = input("์ด๋ค ๋จ์ด๊ฐ ์ผ๋ง๋งํผ ๋์๋์ง๋ ํ์ธํ๊ณ ์ถ๋์? yes/no")
if yes_or_no == "yes":
with open('๋จ์ด๋น๋์_'+ str(now_3) + '.csv', 'w') as f:
w = csv.writer(f)
w.writerow(data.keys())
w.writerow(data.values())
print("ํ์ผ์ด ์ ์ฅ๋์์ต๋๋ค")
else:
pass
yes_or_no == ""
yes_or_no_2 == ""
# ํ์ด์ง url ํ์์ ๋ง๊ฒ ๋ฐ๊พธ์ด ์ฃผ๋ ํจ์ ๋ง๋ค๊ธฐ
#์
๋ ฅ๋ ์๋ฅผ 1, 11, 21, 31 ...๋ง๋ค์ด ์ฃผ๋ ํจ์
def makePgNum(num):
if num == 1:
return num
elif num == 0:
return num+1
else:
return num+9*(num-1)
# ํฌ๋กค๋งํ url ์์ฑํ๋ ํจ์ ๋ง๋ค๊ธฐ(๊ฒ์์ด, ํฌ๋กค๋ง ์์ ํ์ด์ง, ํฌ๋กค๋ง ์ข
๋ฃ ํ์ด์ง)
def makeUrl(search, start_pg, end_pg):
if start_pg == end_pg:
start_page = makePgNum(start_pg)
global url
url = "https://search.naver.com/search.naver?where=news&sm=tab_pge&query=" + search + "&start=" + str(start_page)
return url
else:
urls = []
for i in range(start_pg, end_pg + 1):
page = makePgNum(i)
url = "https://search.naver.com/search.naver?where=news&sm=tab_pge&query=" + search + "&start=" + str(page)
urls.append(url)
print("์์ฑ url ๊ฐ๊ณต์ค... ์๊ฐ์ด ๊ฑธ๋ฆฝ๋๋ค")
return urls
def makeUrl_2(search, start_pg, end_pg):
if start_pg == end_pg:
start_page = makePgNum(start_pg)
url = "https://search.naver.com/search.naver?where=news&sm=tab_pge&query=" + search + "&sort=1&start=" + str(start_page)
return url
else:
urls = []
for i in range(start_pg, end_pg + 1):
page = makePgNum(i)
url = "https://search.naver.com/search.naver?where=news&sm=tab_pge&query=" + search + "&sort=1&start=" + str(page)
urls.append(url)
print("์์ฑ url ๊ฐ๊ณต์ค... ์๊ฐ์ด ๊ฑธ๋ฆฝ๋๋ค")
return urls
# html์์ ์ํ๋ ์์ฑ ์ถ์ถํ๋ ํจ์ ๋ง๋ค๊ธฐ (๊ธฐ์ฌ, ์ถ์ถํ๋ ค๋ ์์ฑ๊ฐ)
def news_attrs_crawler(articles,attrs):
attrs_content=[]
for i in articles:
attrs_content.append(i.attrs[attrs])
return attrs_content
# ConnectionError๋ฐฉ์ง
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4758.102"}
def crawling():
#####๋ด์คํฌ๋กค๋ง ์์#####
# ๊ฒ์์ด ์
๋ ฅ
global search
search = input("๊ฒ์ํ ํค์๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์:")
# ๊ฒ์ ์์ํ ํ์ด์ง ์
๋ ฅ
global page
print("1์ชฝ ๋น 10๊ฐ์ ๊ธฐ์ฌ์
๋๋ค. 200๊ฐ์ ๊ธฐ์ฌ๊ฐ ํ์ํ๋ฉด ์์1-์ข
๋ฃ20")
page = int(input("\nํฌ๋กค๋งํ ์์ ํ์ด์ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์. ex)1(์ซ์๋ง์
๋ ฅ):")) # ex)1 =1ํ์ด์ง,2=2ํ์ด์ง...
print("\nํฌ๋กค๋งํ ์์ ํ์ด์ง: ", page, "ํ์ด์ง")
# ๊ฒ์ ์ข
๋ฃํ ํ์ด์ง ์
๋ ฅ
global page2
page2 = int(input("\nํฌ๋กค๋งํ ์ข
๋ฃ ํ์ด์ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์. ex)1(์ซ์๋ง์
๋ ฅ):")) # ex)1 =1ํ์ด์ง,2=2ํ์ด์ง...
# naver url ์์ฑ
search_option = input("์ด๋ค ์์ผ๋ก ๊ฒ์ํ์ค๊ฑด๊ฐ์? ๊ธฐ๋ณธ๊ฐ์ ์ต์ ์์
๋๋ค. 1. ์ธ๊ธฐ์ 2. ์ต์ ์ ์ซ์๋ง ์
๋ ฅ: ")
global url
if search_option == "1":
url = makeUrl(search, page, page2)
else:
url = makeUrl_2(search, page, page2)
# ๋ด์ค ํฌ๋กค๋ฌ ์คํ
global news_titles
news_titles = []
global news_url
news_url = []
global news_contents
news_contents = []
global news_dates
news_dates = []
for i in url:
original_html = requests.get(i, headers=headers)
html = BeautifulSoup(original_html.text, "html.parser")
url_naver = html.select(
"div.group_news > ul.list_news > li div.news_area > div.news_info > div.info_group > a.info")
url = news_attrs_crawler(url_naver, 'href')
news_url.append(url)
# ์ ๋ชฉ, ๋งํฌ, ๋ด์ฉ ๋ด์ ๋ฆฌ์คํธ ์์ฑ
global news_url_1
news_url_1 = []
# 1์ฐจ์ ๋ฆฌ์คํธ๋ก ๋ง๋ค๊ธฐ(๋ด์ฉ ์ ์ธ)
for i in news_url:
for j in i:
news_url_1.append(j)
# NAVER ๋ด์ค๋ง ๋จ๊ธฐ๊ธฐ
global final_urls
final_urls = []
for i in range(len(news_url_1)):
if "news.naver.com" in news_url_1[i]:
final_urls.append(news_url_1[i])
else:
pass
print("๋ค์ด๋ฒ ๋ด์ค๋ง ์์๋ด๋ ์ค...")
# ๋ด์ค ๋ด์ฉ ํฌ๋กค๋ง
for i in final_urls:
global progress
progress = progress + 1
print(str(progress) + "/ ์ต๋" + str((page2 + 1 - page) * 10) + " ํญ๋ชฉ ์ฒ๋ฆฌ์ค")
# ๊ฐ ๊ธฐ์ฌ html getํ๊ธฐ
news = requests.get(i, headers=headers)
news_html = BeautifulSoup(news.text, "html.parser")
# ๋ด์ค ์ ๋ชฉ ๊ฐ์ ธ์ค๊ธฐ
title = news_html.select_one("#ct > div.media_end_head.go_trans > div.media_end_head_title > h2")
if title == None:
title = news_html.select_one("#content > div.end_ct > div > h2")
# ๋ด์ค ๋ณธ๋ฌธ ๊ฐ์ ธ์ค๊ธฐ
content = news_html.select("div#dic_area")
if content == []:
content = news_html.select("#newsct_article")
# ๊ธฐ์ฌ ํ
์คํธ๋ง ๊ฐ์ ธ์ค๊ธฐ
# listํฉ์น๊ธฐ
content = ''.join(str(content))
# htmlํ๊ทธ์ ๊ฑฐ ๋ฐ ํ
์คํธ ๋ค๋ฌ๊ธฐ
pattern1 = '<[^>]*>'
title = re.sub(pattern=pattern1, repl='', string=str(title))
content = re.sub(pattern=pattern1, repl='', string=content)
pattern2 = """[\n\n\n\n\n// flash ์ค๋ฅ๋ฅผ ์ฐํํ๊ธฐ ์ํ ํจ์ ์ถ๊ฐ\nfunction _flash_removeCallback() {}"""
content = content.replace(pattern2, '').replace("[", "").replace("]", "")
news_titles.append(title)
news_contents.append(content)
global news_date
try:
html_date = news_html.select_one("div#ct> div.media_end_head.go_trans > div.media_end_head_info.nv_notrans > div.media_end_head_info_datestamp > div > span")
news_date = html_date.attrs['data-date-time']
except AttributeError:
news_date = news_html.select_one("#content > div.end_ct > div > div.article_info > span > em")
news_date = re.sub(pattern=pattern1, repl='', string=str(news_date))
# ๋ ์ง ๊ฐ์ ธ์ค๊ธฐ
news_dates.append(news_date)
print("\n๊ฒ์๋ ์๋ ๊ฐฏ์: ์ด ", (page2 + 1 - page), 'ํ์ด์ง')
print("์ฑ๊ณตํ ๊ฐฏ์:" + str(progress))
print("๊ฒ์ ์ฑ๊ณต ๊ฐฏ์๊ฐ ์๋ ๊ฐฏ์๋ณด๋ค ์ ์ ๊ฒฝ์ฐ ๊ฒ์ ํ์ด์ง ์๋ฅผ ๋๋ ค์ ์๋ํด๋ณด์ธ์\n")
print('news_title: ', len(news_titles))
print('news_url: ', len(final_urls))
print('news_contents: ', len(news_contents))
print('news_dates: ', len(news_dates))
###๋ฐ์ดํฐ ํ๋ ์์ผ๋ก ๋ง๋ค๊ธฐ###
#๋ฐ์ดํฐ ํ๋ ์ ๋ง๋ค๊ธฐ
global news_df
news_df = pd.DataFrame({'date':news_dates,'title':news_titles,'link':final_urls,'content':news_contents})
news_df
#์ค๋ณต ํ ์ง์ฐ๊ธฐ
news_df = news_df.drop_duplicates(keep='first',ignore_index=True)
print("์ค๋ณต ์ ๊ฑฐ ํ ํ ๊ฐ์: ",len(news_df))
#๋ฐ์ดํฐ ํ๋ ์ ์ ์ฅ
now = datetime.datetime.now()
news_df.to_csv('{}_{}.csv'.format(search,now.strftime('%Y%m%d_%H์%M๋ถ%S์ด')),encoding='utf-8-sig',index=False)
# ==============================================================================
print("๋ค์ด๋ฒ ๋ด์ค ์ปจํ
์ธ ํฌ๋กค๋ง ํ๋ก๊ทธ๋จ์
๋๋ค.")
print("๋ฒ์ ์ ๋ณด: 2022-12-07 ver\n")
exit_or_not = "yes"
while exit_or_not == "yes" or exit_or_not == "y":
what = input("\n์ด๋ค ๊ฒ์ ํ์๊ฒ ์ต๋๊น? 1. ์๋ํด๋ผ์ฐ๋ ๋ณ๊ฒฝ 2. ๋ด์ค ํฌ๋กค๋ง *๋ฒํธ๋ง ์
๋ ฅํด์ฃผ์ธ์*: ")
if what == "1":
wordcloud()
elif what == "2":
progress = 0
crawling()
else:
print("์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค. ์ฒ์์ผ๋ก ๋์๊ฐ๋๋ค\n")
pass
print("์๋ฃ๋์์ต๋๋ค. ์ฒ์์ผ๋ก ๋์๊ฐ๋ ค๋ฉด yes ๋ y ๋ฅผ, ์ข
๋ฃํ๋ ค๋ฉด ์๋ฌดํค๋ ๋๋ฌ์ฃผ์ธ์")
what = input()
sys.exit()