-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeDownload.py
More file actions
133 lines (103 loc) · 3.92 KB
/
Copy pathYoutubeDownload.py
File metadata and controls
133 lines (103 loc) · 3.92 KB
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
#!/usr/bin/python
# coding=utf-8
import sys
import zipfile
import shutil
import os
import os.path
import time
import datetime
import json
import requests
#include common.py
#
o_path = os.getcwd() # 返回当前工作目录
print(o_path)
# sys.path.append(o_path) # 添加自己指定的搜索路径
# 当前工作目录 Common/PythonCreator/ProjectConfig/Script
sys.path.append('../../')
# sys.path.append('./')
dir = os.path.abspath(__file__)
print(dir)
dir = os.path.dirname(dir)
print(dir)
# sys.path.append("..") #把上级目录加入到变量中
sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Common.Common import Common
# from Config import config
from Common import Source
# from Config import adconfig
from Common.File.FileUtil import FileUtil
from Common.Platform import Platform
from Common.WebDriver.WebDriverCmd import CmdType
from Common.WebDriver.WebDriverCmd import WebDriverCmd
from Common.WebDriver.WebDriverCmd import CmdInfo
# https://www.jianshu.com/p/e18cdb1053d0
# pip3 install pytube
# pytube http://youtu.be/rJ18V_NNq8g
from pytube import YouTube
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium import webdriver
# urllib.error.URLError: <urlopen error [Errno 61] Connection refused>
# Youtube视频下载网站 https://zh.savefrom.net/7/
class YoutubeDownload():
driver: None
URL_HEAD = "http://47.242.56.146"
URL_PORT = "5001"
#构造函数
def __init__(self):
name =""
self.Init()
def Init(self):
# 创建chrome浏览器驱动,无头模式(超爽)
chrome_options = Options()
# chrome_options.add_argument('--headless')
self.driver = webdriver.Chrome(chrome_options=chrome_options)
# 全屏
self.driver.maximize_window()
# 具体大小
# driver.set_window_size(width, height)
# # 快照显示已经成功登录
# print(driver.save_screenshot('jietu.png'))
# driver.quit()
# pytube http://youtu.be/rJ18V_NNq8g --itag=22
# http://youtu.be/rJ18V_NNq8g
def Download(self, url,savefilepath):
print("YoutubeDownload Download url=",url)
# url = "http://youtu.be/rJ18V_NNq8g"
# YouTube(url).streams.first().download()
# self.DownloadByBrowser(url)
self.DownloadServer(url,savefilepath)
def DownloadServer(self, url,savefilepath):
# http://47.242.56.146:5001/YoutubDownload?keyid=rJ18V_NNq8g
idx = url.rfind("/")
idx=idx+1
keyid = url[idx:]
url_server = self.URL_HEAD+":"+self.URL_PORT+"/YoutubDownload?keyid="+keyid
print("YoutubeDownload url_server=",url_server)
name = self.GetUrl(url_server)
url_video = self.URL_HEAD+"/"+name
print("YoutubeDownload url_video=",url_video)
r = requests.get(url_video)
with open(savefilepath, "wb") as code:
code.write(r.content)
def GetUrl(self, url):
r = requests.get(url)
return r.content.decode('utf-8',"ignore")
# Youtube视频下载网站 https://zh.savefrom.net/7/
def DownloadByBrowser(self, url,savefilepath):
# 加载百度页面
self.driver.get("https://zh.savefrom.net/7/")
time.sleep(1)
webcmd = WebDriverCmd(self.driver)
key = "//input[@id='sf_url']"
webcmd.AddCmdWait(CmdType.INPUT,key,url)
# <input type="text" name="sf_url" value="" autofocus="" placeholder="在这里粘贴视频链接" onfocus="if(this.value && this.select){this.select()}" id="sf_url">
# <button type="submit" name="sf_submit" class="submit" id="sf_submit">下载</button>
key = "//button[@id='sf_submit']"
webcmd.AddCmd(CmdType.CLICK,key)
webcmd.Run(True)
mainYoutubeDownload = YoutubeDownload()