-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_channel_downloads.py
81 lines (73 loc) · 2.63 KB
/
youtube_channel_downloads.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
import requests
from bs4 import BeautifulSoup
import re
import subprocess
base_url = 'https://www.youtube.com/watch?v='
url = []
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6',
'x-client-data': 'CI62yQEIpLbJAQjBtskBCKmdygEI2J3KAQjancoBCKijygE='
}
totalVideo = re.compile('"ytInitialData.*?totalVideos.*?:(\d+),"ownerName"', re.S)
videoIds = re.compile('"watchEndpoint":{"videoId":"(.*?)",', re.S)
list_patten = re.compile('list=(.*?)$',re.S)
ids = []
downUrl = []
def parser_html(url):
try:
response = requests.get(url,headers=headers)
if response.status_code == 200:
Bsp = BeautifulSoup(response.text, 'html.parser')
scripts = Bsp.select('script')
for script in scripts:
if 'watchEndpoint'in script.text:
str = script.text.strip()
return str
except requests.RequestException as e:
parser_html(url)
def total_index(str):
try:
nums = re.findall(totalVideo, str)
return int(nums[0])
except Exception as e:
print(e)
def video_ID(str):
global ids
try:
id = re.findall(videoIds,str)
for i in id:
if i not in ids and len(i) < 12:
ids.append(i)
except Exception as e:
print(e)
def main(path,url):
path_re = path.replace('\\','/')
save = path_re+'/'+'%(title)s-%(id)s.%(ext)s'
global ids, down
str = parser_html(url)
video_ID(str)
nums = total_index(str)
if nums/79.0>1:
list_id = re.findall(list_patten,url)[0]
for i in range(int(nums/79.0)):
len_list = len(ids)
new_url = 'https://www.youtube.com/watch?v={0}&index={1}&list={2}'.format(ids[int(len_list)-1],int(len_list)-4,list_id)
str = parser_html(new_url)
video_ID(str)
for id in ids:
downUrl.append(base_url+id)
print('下载开始')
try:
for down in downUrl:
action = subprocess.Popen('youtube-dl -c -l -o '+save+' '+down, shell=True, stdout=subprocess.PIPE)
print(action.stdout.read().decode('utf-8'))
except Exception as e:
print(e)
print('下载当前链接出错{0}'.format(down))
print('下载结束')
if __name__ == '__main__':
url = input('please input youtube list url : ')
path = input('input path to save video: ')
main(path,url)