【爬虫脚本】实现批量pdf文件下载

【爬虫脚本】实现批量pdf文件下载

xlwin136 人工智能教学实践 2025年03月30日 14:09

【爬虫脚本】实现批量pdf文件下载

python 复制代码
import requests
from bs4 import BeautifulSoup
import os
from urllib.parse import urljoin, unquote
import re
import chardet
import signal

# 修复Ctrl+C中断问题
signal.signal(signal.SIGINT, lambda sig, frame: exit(0))


def download_all_pdfs(url, save_dir='downloads'):
    os.makedirs(save_dir, exist_ok=True)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

    try:
        # 自动检测网页编码
        response = requests.get(url, headers=headers, timeout=10)
        encoding = chardet.detect(response.content)['encoding'] or 'gbk'
        response.encoding = encoding  # 关键修复

        soup = BeautifulSoup(response.text, 'html.parser')

        for link in soup.find_all('a', href=re.compile(r'\.pdf$')):
            try:
                # 双保险文件名提取
                filename = link.get('title', '').strip() or unquote(link['href'].split('/')[-1])

                # 多层解码处理
                for _ in range(3):
                    filename = unquote(filename)

                # 清洗文件名
                filename = re.sub(r'[\\/*?:"<>|]', '_', filename)  # 替换非法字符
                filename = filename.replace(' ', '_')  # 替换空格
                filename = filename.replace('、', '_')  # 替换全角符号

                # 确保扩展名正确
                if not filename.endswith('.pdf'):
                    filename += '.pdf'

                # 唯一文件名处理
                counter = 1
                base, ext = os.path.splitext(filename)
                while os.path.exists(os.path.join(save_dir, filename)):
                    filename = f"{base}_{counter}{ext}"
                    counter += 1

                save_path = os.path.join(save_dir, filename)

                # 检查并下载
                if os.path.exists(save_path):
                    print(f"跳过已存在文件:{filename}")
                    continue

                full_url = urljoin(url, link['href'])
                with requests.get(full_url, headers=headers, stream=True, timeout=15) as r:
                    r.raise_for_status()
                    with open(save_path, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=8192):
                            f.write(chunk)

                print(f"成功下载:{filename}")

            except Exception as e:
                print(f"下载失败:{full_url},原因:{str(e)}")

    except requests.exceptions.RequestException as e:
        print(f"网页请求失败:{e}")


if __name__ == "__main__":
    target_url = 'http://www.hebeea.edu.cn/html/mtyj/2025/0211-193727-999.html'
    download_all_pdfs(target_url)

下载了一堆考试说明文档

相关推荐
小周同学:20 小时前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu1 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
迪尔~2 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
忆~遂愿3 天前
Python实战教程:PDF文档自动化编辑与图表绘制全攻略
python·pdf·自动化
Rust语言中文社区3 天前
简单好用的在线工具:轻松把图片添加到 PDF
pdf
鲁班AI3 天前
pdf怎么转换成ppt?AI工具与传统方法深度对比
人工智能·pdf·powerpoint
大山运维3 天前
免费专业PDF文档扫描效果生成器
pdf
伊织code4 天前
pdftk - macOS 上安装使用
macos·pdf·pdftk
菜鸟-要努力4 天前
pdf文件转word免费使用几个工具
pdf·word
量化风云5 天前
『量化人的概率 03』PDF is all you need
python·金融·pdf·概率论·量化交易·量化课程