-
Notifications
You must be signed in to change notification settings - Fork 6
/
youtube_playlist.py
31 lines (27 loc) · 979 Bytes
/
youtube_playlist.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
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
PLAYLIST = 'https://www.youtube.com/watch?v=FNQxxpM1yOs&list=PLQVvvaa0QuDeA05ZouE4OzDYLHY-XH-Nd'
TITLE_START = 0
DOMAIN = 'https://www.youtube.com/'
titles = []
links = []
def crawl(url):
content = requests.get(url).text
soup = BeautifulSoup(content, "html.parser")
try:
for playlist in soup.findAll('div',
{'class': 'playlist-videos-container'}):
for link in playlist.findAll('a'):
results = urljoin(DOMAIN, link.get('href'))
# links.append(results)
print(results)
for title in playlist.findAll('h4'):
title = title.string.strip()
title = title.replace("'", "")
title = title[TITLE_START:].strip()
# titles.append(title)
print(title)
except:
print('Error!')
crawl(PLAYLIST)