Last active
November 8, 2017 06:30
-
-
Save datalater/936c6a9679eb2f6e582e63c09dee2eb4 to your computer and use it in GitHub Desktop.
youtube_playlist_parsing_videotitle_and_link_and_download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import csv | |
from bs4 import BeautifulSoup | |
from urllib.parse import urljoin | |
# import youtube_dl # pip install --upgrade youtube-dl | |
list_url = "https://www.youtube.com/playlist?list=PL3FW7Lu3i5Jsnh1rnUwq_TcylNr7EkRe6" | |
youtube_url = "https://www.youtube.com" | |
resp = requests.get(list_url).text | |
soup = BeautifulSoup(resp, 'lxml') | |
###################### | |
### playlist title ### | |
###################### | |
filename = soup.find('title').string | |
filename = filename.replace(' - YouTube',"") | |
filename = filename.replace(":","-").replace("/","-").replace("\\","-").replace("?","-").replace("*","-").replace("\"","-").replace("<","-").replace(">","-").replace("|","-") | |
print(filename) | |
title_dic = {} | |
for i, tr in enumerate(soup.select('.yt-uix-tile')): | |
data_title = tr.get('data-title') | |
data_title = data_title.replace(":","-").replace("/","-").replace("\\","-").replace("?","-").replace("*","-").replace("\"","-").replace("<","-").replace(">","-").replace("|","-") | |
href = tr.find('a')['href'] | |
link = urljoin(youtube_url, href) | |
print(str(i+1).zfill(2),data_title, link) | |
new_title = str(i+1).zfill(2) + "-" + data_title+".mp4" | |
new_title = new_title.replace(":","-").replace("/","-").replace("\\","-").replace("?","-").replace("*","-").replace("\"","-").replace("<","-").replace(">","-").replace("|","-") | |
##################################################### | |
### make csv file for using 4k video downloader ### | |
### download url : https://www.4kdownload.com/ko/ ### | |
##################################################### | |
with open(filename + '.csv', 'a', newline='') as f: | |
r_writer = csv.writer(f) | |
r_writer.writerow([link]) | |
############################################################### | |
### make csv file for file-renaming using Ant Renamer2 ### | |
### download url : https://www.fosshub.com/Ant-Renamer.html ### | |
############################################################### | |
with open(filename + "-for-renaming" + '.csv', 'a', newline='') as f: | |
r_writer = csv.writer(f) | |
r_writer.writerow([data_title, new_title]) | |
################################################################ | |
### video download with customized filename using youtube_dl ### | |
################################################################ | |
# ydl_opts = { | |
# 'outtmpl': str(i+1).zfill(2)+"-"+data_title+".mp4" | |
# } | |
# with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
# ydl.download([link]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment