提问人:Sneider 提问时间:10/30/2023 最后编辑:Charles DuffySneider 更新时间:10/30/2023 访问量:39
用于从 YouTube 频道下载视频的自动化代码存在问题
problem with an automation code for downloading videos from a YouTube channel
问:
大家下午好,我对 Python 中的一些代码有疑问。运行代码时,视频不会保存在我希望它们保存的文件夹中,它只打开频道页面,没有其他内容。有人可以帮我解决这个问题吗?
这是代码:(在频道链接中,频道链接将转到)
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import time
from random import randint
import requests
from pytube import YouTube
# Open browser
driver = webdriver.Firefox()
url = "https://channel_link"
#Get youtube video
def get_video_youtube(driver,url):
driver.get(url)
time.sleep(randint(5, 9))
driver.get(url+"/videos")
ht = driver.execute_script("return document.documentElement.scrollHeight;")
while True:
prev_ht = driver.execute_script("return document.documentElement.scrollHeight;")
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(3)
ht = driver.execute_script("return document.documentElement.scrollHeight;")
# element = driver.find_element(By.XPATH, "//input[@name='q']")
if (prev_ht == ht):
break
links = driver.find_elements(By.XPATH, "//input[@id='video-title']")
with open('d:\\somefile.txt', 'w') as the_file:
for link in links:
print(link.get_attribute("title"))
print(link.get_attribute("href"))
yt = YouTube(link.get_attribute("href"))
hd = yt.streams.get_highest_resolution()
hd.download("D:\\youtube1\\")
the_file.writelines(link.get_attribute("href")+'\n')
sleep(3)
get_video_youtube(driver, url)
然后,pycharm 不会生成任何错误,它只是运行并完成,但不会保存任何内容
答: 暂无答案
评论