-
Notifications
You must be signed in to change notification settings - Fork 11
/
youtube_channel_scrap.py
293 lines (231 loc) · 9.51 KB
/
youtube_channel_scrap.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
from bs4 import BeautifulSoup
import re
import requests
import csv
import time
import json
# version 1.1 added handling of youtube.com/channel/
# to already handling of youtube.com/user based channels
# NOTE: url gets to youtube are throttled to 3 seconds between requests
# this is an ad hoc attempt to look like a human to youtube
# so youtube does not start limiting access
wait_between_requests = 3
""" scrape youtube channel to build table of contents html file and
csv of video information for excel file
note this code has a slow down delay to meet youtube terms of use
"""
# set youtube channel name here
channel_name = 'gjenkinslbcc'
youtube_base = 'https://www.youtube.com/'
# others to try
# gotreehouse howgrowvideo gjenkinslbcc howgrowvideo
# by channel name:
# UCu8YylsPiu9XfaQC74Hr_Gw UCn34N9fj3x92kOGnQQdHkAQ
# UCH4aPBlmmW1Vgs0ykktCMUg
parent_folder = '' # users or channel or empty
def get_soup(url):
"""open url and return BeautifulSoup object,
or None if site does not exist"""
result = requests.get(url)
if result.status_code != 200: return None
time.sleep(wait_between_requests) # slow down to human speed
return BeautifulSoup(result.text, 'html.parser')
def channel_section_links(channel_name):
"""list of
{ 'title': <section title>,
'link': <url to section play lists>
}"""
global parent_folder
soup = get_soup(f'{youtube_base}/user/{channel_name}/playlists')
if soup is None or 'This channel does not exist.' in soup.text:
url = f'{youtube_base}/channel/{channel_name}/playlists'
soup = get_soup(url)
if soup is None or 'This channel does not exist.' in soup.text:
raise ValueError(
'The channel does not exists: ' + channel_name)
parent_folder = 'channel/'
play_list_atags = \
soup.find_all('a',
{'href': re.compile(f'{channel_name}/playlists')})
# filter out non user play lists next
elements = [{'title': x.text.strip(),
'link': fix_url(x['href'])} for x in play_list_atags
if x.span and
('shelf_id=0' not in x['href'])]
# no sections, make up no sections section with default link
if len(elements) == 0:
url = f'{youtube_base}{parent_folder}{channel_name}/playlists'
elements = [ {'title': 'no sections', 'link': url}]
# i.e. https://youtube.com/gotreehouse/playlists
return elements
def fix_url(url): # correct relative urls back to absolute urls
if url[0] == '/':
return youtube_base + url
else:
return url
def get_playlists(section):
"""returns list of list of
{ 'title': <playlist tile>, <link to all playlist videos> }"""
global parent_folder
print(f" getting playlists for section: {section['title']}")
soup = get_soup(section['link'])
if soup is None: # no playlist, create dummy with default link
url = f'{youtube_base}{parent_folder}{channel_name}/videos'
return [
{'title': 'No Playlists', 'link':url }]
atags = soup('a', class_='yt-uix-tile-link')
playlists = []
for a in atags: # find title and link
title = a.text
if title != 'Liked videos': # skip these
url = fix_url(a['href'])
playlists.append({'title': title, 'link': url})
if not playlists: # no playlists
url = f'{youtube_base}/{parent_folder}{channel_name}/videos'
return [{'title': 'No Playlists', 'link': url}]
return playlists
def parse_video(vurl):
# return dict of
# title, link, views, publication_date,
# description, short_link, likes, dislikes
d = {'link': vurl, 'views': None, 'short_link': vurl,
'likes': None, 'dislikes': None}
# now get video page and pull information from it
vsoup = get_soup(vurl)
o = vsoup.find('title')
vtitle = o.text.strip()
xending = ' - YouTube'
d['title'] = vtitle[:-len(xending)] \
if vtitle.endswith(xending) else vtitle
print(f" processing video '{d['title']}'" )
# o is used in the code following to
# catch missing data targets for scrapping
o = vsoup.find('div', class_='watch-view-count')
if o:
views = o.text
d['views'] = ''.join(c for c in views if c in '0123456789')
o = vsoup.find('strong', class_='watch-time-text')
d['publication_date'] = \
o.text[len('Published on ') - 1:] if o else ''
o = vsoup.find('div', id='watch-description-text')
d['description'] = o.text if o else ''
o = vsoup.find('meta', itemprop='videoId')
if o:
vid = o['content']
d['short_link'] = f'https://youtu.be/{vid}'
o = vsoup.find('button',
class_='like-button-renderer-like-button')
if o:
o = o.find('span', class_='yt-uix-button-content')
d['likes'] = o.text if o else ''
o = vsoup.find('button',
class_='like-button-renderer-dislike-button')
if o:
o = o.find('span', class_='yt-uix-button-content')
d['dislikes'] = o.text if o else ''
return d
def add_videos(playlist):
"""find videos in playlist[link]
and add their info as playlist[videos] as list"""
surl = playlist['link']
soup = get_soup(surl)
print(f" getting videos for playlist: {playlist['title']}")
videos = []
# items are list of video a links from list
items = soup('a', class_='yt-uix-tile-link')
# note first part of look get info from playlist page item,
# and the the last part opens the video and gets more details
if len(items) > 0:
for i in items:
d = dict()
vurl = fix_url(i['href'])
t = i.find_next('span', {'aria-label': True})
d['time'] = t.text if t else 'NA'
d.update(parse_video(vurl))
videos.append(d)
else: # must be only one video
d = {'time': 'NA'}
d.update(parse_video(surl))
videos.append(d)
# add new key to this playlist of list of video infos
playlist['videos'] = videos
print()
def tag(t,c):
return f'<{t}>{c}</{t}>' # return html tag with content
def link(text, url): # return a tag with content and link
return f'<a href="{url}">{text}</a>'
def html_out(channel, sections):
"""create and write channel_name.html file"""
title = f'YouTube Channel {channel}'
f = open(f'{channel}.html','w')
template = ('<!doctype html>\n<html lang="en">\n<head>\n'
'<meta charset="utf-8">'
'<title>{}</title>\n</head>\n'
'<body>\n{}\n</body>\n</html>')
parts = list()
parts.append(tag('h1', title))
for s in sections:
parts.append(tag('h2',link(s['title'], s['link'])))
for pl in s['playlists']:
parts.append(tag('h3', link(pl['title'], pl['link'])))
if len(pl) == 0:
parts.append('<p>Empty Playlist</p>')
else:
parts.append('<ol>')
for v in pl['videos']:
t = '' if v['time'] == 'NA' else f" ({v['time']})"
parts.append(tag('li', link(v['title'],
v['short_link']) + t))
parts.append('</ol>')
f.write(template.format(channel, '\n'.join(parts)))
f.close()
def csv_out(channel, sections):
""" create and output channel_name.csv
file for import into a spreadsheet or DB"""
headers = ('channel,section,playlist,video,'
'link,time,views,publication date,'
'likes,dislikes,description').split(',')
with open(f'{channel}.csv', 'w') as csv_file:
csvf = csv.writer(csv_file, delimiter=',')
csvf.writerow(headers)
for section in sections:
for playlist in section['playlists']:
for video in playlist['videos']:
v = video
line = [channel,
section['title'],
playlist['title'],
v['title']]
line.extend([v['short_link'],
v['time'], v['views'],
v['publication_date'],
v['likes'], v['dislikes'],
v['description']])
csvf.writerow(line)
def process_channel(channel_name):
sections = channel_section_links(channel_name)
for section in sections:
section['playlists'] = get_playlists(section)
for playlist in section['playlists']:
add_videos(playlist)
return sections
if __name__ == '__main__':
# find channel name by going to channel
# and picking last element from channel url
# for example my channel url is:
# https://www.youtube.com/user/gjenkinslbcc
# my channel name is gjenkinslbcc in this url
# this is set near top of this file
# if the channel is of the form:
# https://www.youtube.com/channel/xyz then supply xyz
print(f'finding sections for youtube.com {channel_name}')
sections = process_channel(channel_name)
# save sections structure to json file
with open(f'{channel_name}.json','w') as f:
f.write(json.dumps(sections, sort_keys=True, indent=4))
html_out(channel_name, sections) # create web page of channel links
# create a csv file of video info for import into spreadsheet
csv_out(channel_name, sections)
print(f"Program Complete,\n '{channel_name}.html' and"
f" '{channel_name}.csv' have been"
f" written to current directory")