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

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

相关推荐
Eiceblue2 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
weixin_527550402 小时前
初级程序员入门指南
javascript·python·算法
程序员的世界你不懂2 小时前
Appium+python自动化(十)- 元素定位
python·appium·自动化
CryptoPP3 小时前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
树叶@3 小时前
Python数据分析7
开发语言·python
老胖闲聊4 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点4 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
浠寒AI4 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python
行云流水剑6 小时前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
心扬6 小时前
python生成器
开发语言·python