forked from my-creator/my-creator-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl2.py
209 lines (154 loc) · 6.94 KB
/
crawl2.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
from bs4 import BeautifulSoup
import csv
import requests
import schedule
import time
def new_crawl(channel_id):
new = []
list_url = 'https://www.youtube.com/channel/' + channel_id + '/videos?view=0&sort=dd&flow=grid'
html_doc = requests.get(list_url).text
soup = BeautifulSoup(html_doc,'html.parser')
new_cnt = 0
new_img_cnt = 0
for l in soup.find_all('div',{"class":"yt-lockup-content"}):
newTime = ''
newViewCnt = ''
for t in l.select('a'):
try:
timeAndViewCnt = l.select('ul li')
if '전' in timeAndViewCnt[0].text:
newTime = timeAndViewCnt[0].text
newViewCnt = timeAndViewCnt[1].text.replace('조회수',"").replace('회',"").replace(',',"").replace(' ',"")
else:
newTime = timeAndViewCnt[1].text
newViewCnt = timeAndViewCnt[0].text.replace('조회수',"").replace('회',"").replace(',',"").replace(' ',"")
except (IndexError, ValueError):
#print("hoterr")
newTime = ''
newViewCnt = 0
#print("hoterr")
#print(hot)
pass
if 'watch' in t['href'] and new_cnt < 5:
newLink = "https://www.youtube.com"+t['href']
newTitle = t.text.replace('\n'," ").replace(','," ")
newtmp = [channel_id,newTitle,newLink,newTime,newViewCnt,""]
new.append(newtmp)
new_cnt += 1
else:
break
for a in soup.find_all('div',{"class":"yt-lockup-thumbnail"}):
for tt in a.select('img'):
if new_img_cnt <5 and tt is not None:
try:
#print("hotimg")
new_img_cnt += 1
#썸네일
if tt['src'] is None:
img = ''
else:
img = tt['src']
#print("img")
#print(img)
new[new_img_cnt-1][5] = img
except (IndexError, ValueError):
#print("hotimgerr")
#print(hot_img_cnt-1)
#print(hot)
pass
print("new")
print(new)
return new
def hot_crawl(channel_id):
hot = []
list_url = 'https://www.youtube.com/channel/' + channel_id + '/videos?view=0&sort=p&flow=grid'
html_doc = requests.get(list_url).text
soup = BeautifulSoup(html_doc,'html.parser')
hot_cnt = 0
hot_img_cnt = 0
for l in soup.find_all('div',{"class":"yt-lockup-content"}):
hotTime = ''
hotViewCnt = ''
for t in l.select('a'):
try:
timeAndViewCnt = l.select('ul li')
if '전' in timeAndViewCnt[0].text:
hotTime = timeAndViewCnt[0].text
hotViewCnt = timeAndViewCnt[1].text.replace('조회수',"").replace('회',"").replace(',',"").replace(' ',"")
else:
hotTime = timeAndViewCnt[1].text
hotViewCnt = timeAndViewCnt[0].text.replace('조회수',"").replace('회',"").replace(',',"").replace(' ',"")
except (IndexError, ValueError):
#print("hoterr")
hotTime = ''
hotViewCnt = 0
#print("hoterr")
#print(hot)
pass
if 'watch' in t['href'] and hot_cnt < 5:
hotLink = "https://www.youtube.com"+t['href']
hotTitle = t.text.replace('\n'," ").replace(','," ")
hottmp = [channel_id,hotTitle,hotLink,hotTime,hotViewCnt,""]
hot.append(hottmp)
hot_cnt += 1
else:
break
for a in soup.find_all('div',{"class":"yt-lockup-thumbnail"}):
for tt in a.select('img'):
if hot_img_cnt <5 and tt is not None:
try:
#print("hotimg")
hot_img_cnt += 1
#썸네일
if tt['src'] is None:
img = ''
else:
img = tt['src']
#print("img")
#print(img)
hot[hot_img_cnt-1][5] = img
except (IndexError, ValueError):
#print("hotimgerr")
#print(hot_img_cnt-1)
#print(hot)
pass
print("channel_id")
print(channel_id)
print("hot")
print(hot)
return hot
def crawl_final():
with open('channelCsv.csv', 'r') as f:
data = f.read().split(",")
# data.replace('"',"")
# data.replace('[',"")
# data.replace(']',"")
for i in range(len(data)):
data[i] = data[i].replace('"',"").replace("[","").replace("]","")
print(len(data))
with open('hotoutput.csv', 'w', encoding='utf-8', newline='') as hot_f:
with open('newoutput.csv', 'w', encoding='utf-8', newline='') as new_f:
wr = csv.writer(hot_f)
wrn = csv.writer(new_f)
#for k in range(len(data):
for k in range(len(data)):
print("k")
print(k)
# result = crawl_final(data[k])
result_hot = hot_crawl(data[k])
result_new = new_crawl(data[k])
#print("result")
#print(result)
try:
for i in range(5):
wr.writerow(result_hot[i])
wrn.writerow(result_new[i])
except (IndexError, ValueError):
print("resulterr")
#print(new_img_cnt-1)
print("channel_id")
print(data[k])
pass
#crawl_final()
#매일 10시에 크롤링 CSV 만들기
schedule.every().day.at("22:00").do(crawl_final)