吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2491|回复: 30
收起左侧

[Python 原创] 新东方英语视频下载

  [复制链接]
Yin07 发表于 2025-8-6 20:21
本帖最后由 Yin07 于 2025-8-6 20:33 编辑

目标网站:
[HTML] 纯文本查看 复制代码
https://www.xdf.cn/bbc/yingyu/list_4851_1.html


用到的工具:webdriver
任意浏览器驱动皆可
Edge:
[HTML] 纯文本查看 复制代码
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/?form=MA13LH#installation

Firefox :
[HTML] 纯文本查看 复制代码
https://github.com/mozilla/geckodriver/releases/

Chrome:
[HTML] 纯文本查看 复制代码
http://chromedriver.storage.googleapis.com/index.html 

IE:
[HTML] 纯文本查看 复制代码
http://selenium-release.storage.googleapis.com/index.html

Opera:
[HTML] 纯文本查看 复制代码
https://github.com/operasoftware/operachromiumdriver/releases



模块安装 selenium
[Python] 纯文本查看 复制代码
pip install selenium



思路分析:
首页数据排列整齐,直接爬取


视频链接地址:

值得注意是视频的加载方式为异步加载,直接使用requests是无法得到视频链接的,
在阻止getsource请求后,视频就不会加载了,并且可以看到与jquery.js有关



这里直接用selenium模拟浏览器操作。
由于需要爬取的视频数量多,可以采用多线程
为了实时观察代码的运行情况,可以在特殊位置打印一下


以下使用Edge进行演示:
模块导入
[Python] 纯文本查看 复制代码
import time
import requests
import re
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from pyquery import PyQuery as pq
from selenium.webdriver import Edge
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import os
from threading import Thread
import  threading
from  queue import  Queue
from  concurrent.futures import ThreadPoolExecutor
from  queue import  Empty


selenium的设置:
[Python] 纯文本查看 复制代码
options = Options()
            options.add_argument('--headless')
            #options.add_experimental_option('detach',True) #保持网页
            service = Service(executable_path=r"D:\python\new\msedgedriver.exe")
            driver = webdriver.Edge(service=service, options=options)

为了不影响视觉效果,这里使用浏览器的无头模式
驱动地址最好直接复制,再替换



数据的获取:
[Python] 纯文本查看 复制代码
driver.get(urls)
        time.sleep(5)

先访问链接,通过5秒的等待让视频能够加载
[Python] 纯文本查看 复制代码
 data= driver.page_source
        #print(data)
        #id=driver.find_element(By.CSS_SELECTOR,'div[id^ ="player-con_component_"]')

        link=re.findall('<video webkit-playsinline="" playsinline="" x-webkit-airplay="" x5-playsinline="" preload="preload" autoplay="autoplay" src="(.*?)" style="width: 100%; height: 100%;"></video>',data)
        #print(link)
        #time.sleep(1)

        driver.quit()

使用drive.page_source获取加载5秒后的页面
再用正则表达式获取到视频链接


考虑到有些页面是没有视频的或者是等待的时间过短
写一个判断有助于发现问题所在

[Python] 纯文本查看 复制代码
        if not link:

            print(title+'链接获取失败,请检查延时是否合理')
            with open(r'D:\python\new\errorlist.txt','a',encoding='utf-8') as txt:
                txt.write(title +':'+ urls +'\n')

                
        else:





数据保存:
[Python] 纯文本查看 复制代码
source = requests.get(url =link[0])

                with open('H:/English/'+title+'.mp4','wb') as f:

                    f.write(source.content)



写到这里已经是可以运行的
为了代码的美观舒适可以进行一点小小的修饰

先创建几个全局变量用来存储返回的链接与文件名
[Python] 纯文本查看 复制代码
task_queue1 =Queue() #to getlink
task_queue2 =Queue()

task_queue3=Queue() # to download as link
task_queue4=Queue()


将整个代码分为三段
定义获取所有视频链接所在网页链接:
[Python] 纯文本查看 复制代码
def geturllist():




将返回的链接与文件名传输至全局变量中
[Python] 纯文本查看 复制代码
  for u , n  in zip(links,titles):

            task_queue1.put(u)
            task_queue2.put(n)


打印一下获取到底链接数量,便于查看
[Python] 纯文本查看 复制代码
print('页面链接收集到',task_queue1.qsize(),'条')


定义获取所有视频链接所属链接:
[Python] 纯文本查看 复制代码
def getlink():


获取全局变量中的链接和文件名并发送请求
[Python] 纯文本查看 复制代码
 pageurl = task_queue1.get_nowait()
            name = task_queue2.get_nowait()
            driver.get(pageurl)



将链接传输至全局变量中

[Python] 纯文本查看 复制代码
 task_queue3.put(link)
            task_queue4.put(title)



定义视频下载:
[Python] 纯文本查看 复制代码
def download():


遍历视频下载链接和文件名

[Python] 纯文本查看 复制代码
 while True:

                url = task_queue3.get_nowait()
                caption = task_queue4.get_nowait()



主程序
[Python] 纯文本查看 复制代码
if __name__ == '__main__':

    thread1 = []
    thread2 = []
    thread3=[]

    getlist = Thread(target=geturllist)

    getlist.start()
    thread1.append(getlist)
    for get in thread1:
        getlist.join()

        time.sleep(2)

    for pool in range(5):

        geturl = Thread(target=getlink)

        geturl.start()

        thread2.append(geturl)

    for i in thread2:

        geturl.join()

    downloaddata = Thread(target=download)

    downloaddata.start()
    thread3.append(downloaddata)

    for down in thread3:

        downloaddata.join()

    print(f'第{x}页下载完成')


运行效果:


源代码:
自动化下载测试.zip (2.82 KB, 下载次数: 52)




免费评分

参与人数 13吾爱币 +17 热心值 +10 收起 理由
OopsTeam + 1 我很赞同!
HanJ + 1 + 1 我很赞同!
FYL11162022 + 1 + 1 热心回复!
lanad + 1 + 1 用心讨论,共获提升!
zzf520 + 1 热心回复!
daoye9988 + 1 + 1 热心回复!
psqladm + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
2024X + 1 + 1 谢谢@Thanks!
pyjiujiu + 1 我很赞同!
wangpj520 + 1 我很赞同!
xhcm88888 + 1 + 1 谢谢@Thanks!
hrh123 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
miaowazhongzi + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

mryouan 发表于 2025-8-6 20:58
妥妥的技术活,支持一下
天天涨停天天盈 发表于 2025-8-6 20:31
smartyou 发表于 2025-8-6 20:56
跑了一下午 发表于 2025-8-6 21:01
也算是视频地址嗅探吗
pjyang 发表于 2025-8-6 21:44
感谢分享,是压缩包带密码么
 楼主| Yin07 发表于 2025-8-6 21:59
解压密码就是 52pojie
crack52 发表于 2025-8-6 22:36
感谢分享
springccc 发表于 2025-8-6 23:32
感谢分享
yum0206 发表于 2025-8-6 23:41

感谢分享,一看就是技术活。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-5-14 09:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表