forked from ospTEAM4/OSP_TERM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_keyword.py
189 lines (164 loc) · 5.69 KB
/
google_keyword.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
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from nltk.corpus import stopwords
import re
import requests
import time
import cut_word
# 크롤링 봇 탐지
# from selenium.webdriver.chrome.options import Options
# import chromedriver_autoinstaller
# import subprocess
# import shutil
# try:
# shutil.rmtree(r"c:\chrometemp") #쿠키 / 캐쉬파일 삭제
# except FileNotFoundError:
# pass
# subprocess.Popen(r'C:\Program Files\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\chrometemp"') # 디버거 크롬 구동
# option = Options()
# option.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
# chrome_ver = chromedriver_autoinstaller.get_chrome_version().split('.')[0]
# try:
# driver = webdriver.Chrome(f'./{chrome_ver}/chromedriver.exe', options=option)
# except:
# chromedriver_autoinstaller.install(True)
# driver = webdriver.Chrome(f'./{chrome_ver}/chromedriver.exe', options=option)
# driver.implicitly_wait(10)
def result(request):
url=request
#driver = webdriver.Chrome('chromedriver.exe')
driver = webdriver.Firefox('')
driver.get(url)
driver.implicitly_wait(5)
time.sleep(1.5)
driver.execute_script("window.scrollTo(0,500);")
time.sleep(1.5)
# 유튜브 팝업 닫기
try:
driver.find_element_by_css_selector("#dismiss-button > a").click()
except:
pass
# 페이지 끝까지 스크롤
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(3)
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
break
last_height = new_height
time.sleep(1.5)
driver.execute_script("window.scrollTo(500,0);")
# 대댓글 모두 열기
buttons = driver.find_elements_by_css_selector("#more-replies > a")
time.sleep(1.5)
for button in buttons:
button.send_keys(Keys.ENTER)
time.sleep(1.5)
button.click()
time.sleep(1.5)
# 정보 추출하기
html_source = driver.page_source
soup = BeautifulSoup(html_source, 'html.parser')
id_list = soup.select("div#header-author > h3 > #author-text > span")
comment_list = soup.select("yt-formatted-string#content-text")
id_final = []
comment_final = []
for i in range(len(comment_list)):
temp_id = id_list[i].text
temp_id = temp_id.replace('\n', '')
temp_id = temp_id.replace('\t', '')
temp_id = temp_id.replace(' ', '')
id_final.append(temp_id)
temp_comment = comment_list[i].text
temp_comment = temp_comment.replace('\n', '')
temp_comment = temp_comment.replace('\t', '')
temp_comment = temp_comment.replace(' ', '')
comment_final.append(temp_comment)
#
# print(id_final)s
# print(comment_final)
return comment_final
# f = open('origin.txt', 'w', encoding='UTF-8')
#
# for comment in comment_final:
# f.write(comment + '\n')
#
# f.close()
#######################################################################################
# file = open('origin.txt', 'r', encoding='UTF-8')
# word_dict = {}
#
# keys = []
# values = []
# word_count = {}
#
# line = file.readline()
# stop_words = set(stopwords.words('english'))
# while line:
# sentence = re.sub('[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]', ' ', line)
# sentence = sentence.lower()
# word_list = sentence.split()
# for word in word_list:
# if word not in stop_words:
# if word in word_count:
# word_count[word] += 1
# else:
# word_count[word] = 1
#
# line = file.readline()
#
# sort_list = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# # print(sort_dict)
# file.close()
# f = open('sorted.txt', 'w', encoding='UTF-8')
#
# # print(type(sort_list[1]))
# # print(sort_list[1])
# for i in sort_list:
# string = str(i)
# string = string.strip("("")""'")
# string = string.replace(",", "")
# string = string.replace("'", "")
# f.write(string + '\n')
#
# f.close()
#
# return comment_final
# 텍스트파일 정렬해서 sorted.txt로 붙여넣기
####################################################################
# data = open('accounts/sorted.txt', 'r', encoding='UTF-8')
# with open('accounts/happy.txt', encoding='UTF-8') as likef:
# like_words = likef.read().replace('\n', ' ')
# with open('accounts/sad.txt', encoding='UTF-8') as dislikef:
# dislike_words = dislikef.read().replace('\n', ' ')
# word_dicts = {}
# lines = data.readlines()
# keys = []
# values = []
#
# for line in lines:
# (k, v) = line.split()
#
# keys.append(k)
# int(v)
# values.append(v)
#
# word_dicts = {'key': keys, 'value': values, }
#
# like_num = 0
# dislike_num = 0
# for key in keys:
# if key in like_words:
# print(key + ' ' + 'is like')
# like_num += 1
# elif key in dislike_words:
# print(key + ' ' + 'is dislike')
# dislike_num += 1
# else:
# print(-1)
# word_dict = {'id': id_final, 'comment': comment_final, 'like_num': like_num, 'dislike_num': dislike_num,
# 'key': keys, 'value': values}
# data.close