【爬虫脚本】实现批量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)

下载了一堆考试说明文档

相关推荐
开开心心_Every1 小时前
轻量级PDF阅读器,仅几M大小打开秒开
linux·运维·服务器·安全·macos·pdf·phpstorm
福大大架构师每日一题2 小时前
ragflow v0.25.1 最新版发布:API 统一、PDF 解析性能大幅优化、连接器删除同步全面增强,更新要点一次看懂
pdf·ragflow
cosinmz1 天前
图片太多太乱怎么整理?分享一个我最近常用的图片转 PDF方法
经验分享·小程序·pdf
其实秋天的枫2 天前
2026年新高考英语大纲词汇表3500个电子版PDF(含正序版、乱序版和默写版)
经验分享·pdf
lijfrank2 天前
MacOS 下 VS Code + LaTeX + Skim 双向同步配置
vscode·macos·pdf·latex·mactex
程序员的记录2 天前
AI 实战 - 文档处理(pdf/work/md/txt...)
pdf
Muyuan19982 天前
22.让 RAG Agent 更像真实产品:聊天页面优化、PDF 上传、知识库重建与检索片段展示
python·django·pdf·fastapi
打小就很皮...2 天前
html2canvas + jsPDF 生成 PDF 的踩坑与解决方案总结
前端·pdf
优化控制仿真模型2 天前
27考研数学一、二、三历年真题及答案解析PDF电子版(1987-2026年)
经验分享·pdf
huluang2 天前
解决 Adobe Acrobat 裁剪 PDF 后内容仍存留的问题
pdf