【使用Selenium爬取视频】

使用Selenium爬取视频

先确定网站

先确定你需要爬取的视频在确定网站的url ,因为视频的url可能会多次跳转。所以需要多次检查并且找到真正的url



书写代码:

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
from lxml import etree
import fake_useragent
from selenium.webdriver.edge import options
# 不打开页面的方式
opt = options.Options()
opt.add_argument("--headless")
# 爬取吞噬星空
url = "https://xxxxx/dongman/4925.html"
driver = webdriver.Chrome(opt)
driver.get(url)
time.sleep(3)
# 获得每一集的视频链接 全部的a标签
numbers = 1
a_list = driver.find_elements(By.XPATH,"//div[@class='module-listxxxxxxxt sort-list     selected']/dixxxxx]/div/a")
print(a_list)
for a in a_list:
    detail_url = a.get_attribute("href")
    print(detail_url)
    driver_edge = webdriver.Edge(opt)
    driver_edge.get(detail_url)
    time.sleep(3)
    video_url = driver_edge.find_element(By.XPATH, '//div[@class="player-wrapper"]/xxxxxxx/tr/td/iframe').get_attribute('src')
    print(video_url)
    driver_fox = webdriver.Firefox()
    driver_fox.get(video_url)
    time.sleep(10)
    video = driver_fox.find_element(By.XPATH, '//div[@class="xxxxxxx/video').get_attribute('src')
    print(video)
    print(f"开始爬取第{numbers}集")
    response = requests.get(video,headers={"User-Agent": fake_useragent.UserAgent().random})
    response_content = response.content
    with open(f"./output/xxxxxx第{numbers}集.mp4", "wb") as fp:
        fp.write(response_content)
    numbers = numbers + 1
    driver_fox.close()
    driver_edge.close()
driver.close()

保存结果

分析网站的步骤大致都是很相同的,找到你要的东西,解析他的路径 然后用 requests ,selenium,或bs4进行爬取,最终找到你想要的东西。

== 如有侵权,请找我删除,抱歉==

相关推荐
java技术之路8 分钟前
【免费AI文档助手开发实战系列】基于正则表达式的PDF脱敏python服务构建(一)
python·pdf·pymupdf·免费pdf脱敏
waynaqua9 分钟前
FastAPI开发AI应用五:模型新增图片理解
python·openai·fastapi
waynaqua10 分钟前
FastAPI开发AI应用四:新增豆包、kimi模型
python·fastapi·ai编程
Kusunoki_D2 小时前
PyTorch 环境配置
人工智能·pytorch·python
测试老哥3 小时前
Jmeter自动化性能测试常见问题汇总
自动化测试·软件测试·测试工具·jmeter·接口测试·压力测试·性能测试
知秋丶3 小时前
大模型应用发展与Agent前沿技术趋势(下)
人工智能·python·ai agent
HenryLin4 小时前
美股量化分析系统 - 模块调用流程文档
python
跟橙姐学代码4 小时前
一文读懂 Python 的 JSON 模块:从零到高手的进阶之路
前端·python
躺不平的小刘4 小时前
从YOLOv5到RKNN:零冲突转换YOLOv5模型至RK3588 NPU全指南
linux·python·嵌入式硬件·yolo·conda·pyqt·pip
苦逼IT运维5 小时前
Jenkins + SonarQube 从原理到实战四:Jenkins 与 Gerrit 集成并实现自动任务
运维·git·测试工具·ci/cd·jenkins