import time
from selenium import webdriver
import requests
import os
import json
from moviepy.editor import VideoFileClip, AudioFileClip
from bs4 import BeautifulSoup



# 定义目标网页的URL
url = 'https://www.1819ys.com/movie/45620-0-0.html'

# 使用Selenium打开网页
driver = webdriver.Chrome('path/to/chromedriver.exe')  # 需要提前下载并指定chromedriver的路径
driver.get(url)

# 等待网页加载完成
time.sleep(5)

# 解析网页内容
soup = BeautifulSoup(driver.page_source, 'html.parser')

# 找到所有视频和音频的链接
video_links = soup.select('a[href$='.mp4']')
audio_links = soup.select('a[href$='.mp3']')

# 下载视频和音频文件
for link in video_links:
    video_url = link['href']
    video_title = link.text
    video_response = requests.get(video_url)
    with open(f'{video_title}.mp4', 'wb') as f:
        f.write(video_response.content)

for link in audio_links:
    audio_url = link['href']
    audio_title = link.text
    audio_response = requests.get(audio_url)
    with open(f'{audio_title}.mp3', 'wb') as f:
        f.write(audio_response.content)

# 关闭浏览器
driver.quit()

错误分析:

出现selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager错误的原因是在webdriver.Chrome()函数中传递了错误的参数。应该传递chromedriver的完整路径,而不是字符串'path/to/chromedriver'。

解决方法:

将代码中driver = webdriver.Chrome('path/to/chromedriver') 更改为 driver = webdriver.Chrome('path/to/chromedriver.exe') ,并将'path/to/chromedriver.exe'替换为您下载的chromedriver.exe文件的实际路径。

Python爬虫:Selenium+BeautifulSoup实现视频音频下载

原文地址: https://www.cveoy.top/t/topic/fQ7t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录