1. 课堂派PPT下载限制的痛点分析每次上课最头疼的就是老师把课件上传到课堂派却设置了禁止下载。作为学生我们经常需要课后复习或者把课件打印出来做笔记。但面对一页页无法保存的PPT只能截图或者手动复制效率极低还容易遗漏内容。我遇到过最崩溃的情况是期末复习时发现某章节的PPT在课堂派上被老师临时设置了权限而第二天就要考试。当时只能熬夜手动截图200多页课件第二天考试时眼睛都是花的。这种经历让我下定决心研究自动化解决方案。课堂派这类教育平台通常采用两种限制方式前端禁用右键菜单和快捷键后端校验登录状态和权限动态加载课件内容避免直接获取传统的手动操作不仅耗时还容易触发平台的反爬机制。而使用Python自动化工具可以完美解决这些问题下面我就分享自己摸索出的完整方案。2. 环境准备与工具选型2.1 基础环境配置推荐使用Python 3.8版本太老的版本可能缺少某些库支持。我的开发环境是Windows 10专业版Python 3.9.6Chrome浏览器 103版本需要安装的核心库pip install selenium img2pdf pillow特别注意ChromeDriver版本必须与本地Chrome浏览器匹配。可以在浏览器地址栏输入chrome://version/查看版本号然后到[ChromeDriver官网]下载对应版本。2.2 为什么选择Selenium相比requests等库Selenium的优势在于完全模拟人类操作浏览器行为能处理JavaScript动态加载的内容绕过前端防爬措施更自然支持登录态保持我测试过requestsBeautifulSoup方案但课堂派的PPT内容都是通过AJAX动态加载直接请求接口会返回403错误。而Selenium就像个真实的用户在操作成功率更高。3. 自动化登录与页面导航3.1 模拟登录课堂派首先创建浏览器实例并设置基本参数from selenium import webdriver from selenium.webdriver.chrome.service import Service service Service(你的chromedriver路径) options webdriver.ChromeOptions() options.add_argument(--disable-blink-featuresAutomationControlled) driver webdriver.Chrome(serviceservice, optionsoptions)关键点是添加AutomationControlled参数来隐藏自动化特征。我实测发现不加这个参数容易被检测到。登录功能实现def login(driver, username, password): driver.get(https://www.ketangpai.com) driver.find_element(xpath, //input[placeholder请输入账号]).send_keys(username) driver.find_element(xpath, //input[placeholder请输入密码]).send_keys(password) driver.find_element(xpath, //button[contains(text(),登录)]).click() time.sleep(3) # 等待登录完成3.2 定位PPT课件元素登录后需要找到目标课程和PPT# 进入指定课程 driver.get(课程URL) time.sleep(2) # 点击课件标签 driver.find_element(xpath, //div[contains(text(),课件)]).click() time.sleep(1) # 找到目标PPT并点击 ppt_items driver.find_elements(xpath, //div[classppt-item]) ppt_items[ppt_index].click() # 选择第几个PPT time.sleep(2) # 等待加载4. PPT图片抓取与保存4.1 分析图片加载机制通过开发者工具分析发现课堂派的PPT查看器有以下特点当前页图片的src属性是真实URL前后页图片藏在data-src属性中图片采用懒加载滚动才触发加载因此不能直接获取所有图片需要模拟翻页操作。4.2 自动化翻页抓取def download_ppt_images(driver, save_dir, total_pages): if not os.path.exists(save_dir): os.makedirs(save_dir) next_btn driver.find_element(xpath, //div[contains(class,next-page)]) for page in range(1, total_pages1): # 获取当前页图片 img driver.find_element(xpath, //div[classppt-viewer]//img) img_url img.get_attribute(src) # 下载图片 response requests.get(img_url, streamTrue) with open(f{save_dir}/page_{page}.jpg, wb) as f: for chunk in response.iter_content(1024): f.write(chunk) # 翻页 next_btn.click() time.sleep(1.5) # 等待加载建议在翻页间添加1-2秒间隔避免操作过快被限制。我测试时设置0.5秒间隔连续下载30页后触发了验证码。5. 图片转PDF完整方案5.1 图片排序与预处理下载的图片需要按页码排序from PIL import Image def process_images(image_dir): images [] for img_name in sorted(os.listdir(image_dir)): if img_name.endswith(.jpg): img_path os.path.join(image_dir, img_name) # 统一图片尺寸 img Image.open(img_path) img img.resize((800, 600)) # A4比例 img.save(img_path) images.append(img_path) return sorted(images, keylambda x: int(x.split(_)[-1].split(.)[0]))5.2 使用img2pdf生成PDFimport img2pdf def images_to_pdf(image_paths, output_pdf): with open(output_pdf, wb) as f: f.write(img2pdf.convert(image_paths))如果想调整PDF页面大小可以指定layout函数layout img2pdf.get_layout_fun( (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297)) # A4尺寸 ) f.write(img2pdf.convert(image_paths, layout_funlayout))6. 实战经验与优化建议6.1 反爬应对策略经过多次测试我总结了这些防封技巧随机化操作间隔时间用random.uniform(1,3)代替固定sleep添加鼠标移动轨迹模拟更换User-Agent和浏览器指纹使用代理IP轮换完整的人机行为模拟代码from selenium.webdriver.common.action_chains import ActionChains def human_like_move(driver, element): actions ActionChains(driver) actions.move_to_element_with_offset(element, 5, 5) actions.pause(random.uniform(0.5, 1.5)) actions.click() actions.perform()6.2 性能优化技巧当PPT页数超过100页时可以使用多线程分块下载启用图片压缩减少体积添加断点续传功能我优化后的版本下载200页PPT只需3分钟生成的PDF大小控制在10MB以内。这个方案不仅适用于课堂派稍作修改也能用于超星、智慧树等平台。核心思路都是通过模拟真实用户操作绕过前端限制最终实现课件自由。