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

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

相关推荐
头疼的程序员25 分钟前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
别让别人觉得你做不到1 小时前
Python(1) 做一个随机数的游戏
python
小彭律师2 小时前
人脸识别门禁系统技术文档
python
张小九994 小时前
PyTorch的dataloader制作自定义数据集
人工智能·pytorch·python
zstar-_4 小时前
FreeTex v0.2.0:功能升级/支持Mac
人工智能·python·macos·llm
苏生要努力4 小时前
第九届御网杯网络安全大赛初赛WP
linux·python·网络安全
于壮士hoho4 小时前
DeepSeek | AI需求分析
人工智能·python·ai·需求分析·dash
蒙奇D索大4 小时前
【人工智能】自然语言编程革命:腾讯云CodeBuddy实战5步搭建客户管理系统,效率飙升90%
人工智能·python·django·云计算·腾讯云
AndrewHZ4 小时前
【Python生活】如何构建一个跌倒检测的算法?
python·算法·生活·可视化分析·陀螺仪·加速度计·跌倒检测
lizz6665 小时前
Python查询ES错误ApiError(406, ‘Content-Type ...is not supported
python·elasticsearch