【使用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进行爬取,最终找到你想要的东西。

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

相关推荐
OPEN-F3 小时前
Python进阶教程:Web开发入门(Flask)
开发语言·python
for_ever_love__3 小时前
python基础语法学习: 类型注解
开发语言·python·学习
王志来137944730083 小时前
聚焦五大核心场景:工控服务器机箱平台的垂直深耕之路
运维·服务器·python
zmzb01033 小时前
Python课后习题训练记录Day193
开发语言·python
卷无止境3 小时前
LiteLLM 全面解析开源AI网关如何统一管理百余种大模型
后端·python
我的xiaodoujiao4 小时前
使用PyMySQL模块技术操作MySQL数据库
数据库·python·测试工具·mysql
卷无止境4 小时前
终端里的AI辅助,一场正在发生的编程效率变革
后端·python
苏灿烤鱼4 小时前
上下文写成文件系统,为什么向量还能静默丢?
python·github·agent
SamChan905 小时前
对比 4 种主流 PDF 文档解析方案:PyMuPDF vs pdfplumber vs Apache PDFBox vs 大模型 OCR
python·ai·pdf·ocr·apache·机器翻译
安_13 小时前
如何构建和使用向量索引?HNSW 和 IVF 有什么区别?
python·ai