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

下载了一堆考试说明文档

相关推荐
2601_9677607811 小时前
2026年PDF压缩与页码添加工具技术实测:性能、算法与本地化适配深度对比
算法·pdf
weixin_4508135318 小时前
pdf文档转为markdown文档
linux·pdf
慧都小妮子19 小时前
用 Python 批量把 PDF 参考文献排成 MLA 格式:Spire.Doc for Python 实战
python·pdf·c#·spire.doc·mla格式·参考文献排版·pdf批量处理
weixin_493503671 天前
商会系统里的在线文件预览:PDF/Word/Excel 三种实现与选型(Node + 前端实战)
pdf·word·excel
ygwelcome1 天前
视频收割机-VideoHarvester架构功能介绍
pdf·音视频
2601_967760781 天前
PDF转图片分辨率怎么选?几款工具实测
pdf
runningshark1 天前
Lecture: Simple Present vs Present Continuous PDF
pdf
weixin_416667962 天前
Stirling-PDF是一款完全开源免费、本地部署的全能PDF处理工具
pdf
开开心心就好2 天前
PDF图片去水印软件,支持批量处理页面
前端·javascript·人工智能·智能手机·pdf·语音识别
2501_919749032 天前
华为鸿蒙免费PDF工具—小羊免费PDF
华为·pdf·harmonyos·鸿蒙