python脚本批量导出ZEEKLOG里的文章

python脚本批量导出ZEEKLOG里的文章

一 导出全部已发布文章

首先,需要在本地安装3.8版本以上的python,安装python步骤

检查是否安装成功

pip3 --version 

安装后执行

pip3 install requests beautifulsoup4 markdownify 

新建脚本,脚本名字随意,这里是:ZEEKLOG_downloader.py

脚本内容如下:

# -*- coding: utf-8 -*-import os import re import requests import time from bs4 import BeautifulSoup from markdownify import markdownify as md from urllib.parse import urlparse, unquote import hashlib from pathlib import Path # ================== 配置区 ================== ZEEKLOG_USERNAME ="qq_33417321"# ←←← 修改为你想下载的用户名 SAVE_DIR = Path("ZEEKLOG_articles")# 文章保存根目录(自动跨平台) HEADERS ={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36","Referer":"https://blog.ZEEKLOG.net/"}defsanitize_filename(name:str)->str:"""清理文件名,移除 Windows 非法字符和‘原创’字样""" name = name.replace("原创","").strip()# 移除 Windows 非法字符 name = re.sub(r'[\\/*?:"<>|\r\n]',"_", name)return name or"untitled"defget_article_list(username):"""获取博主文章列表(标题和URL)""" url =f"https://blog.ZEEKLOG.net/{username}/article/list" articles =[] page =1whileTrue: response = requests.get(f"{url}/{page}", headers=HEADERS) soup = BeautifulSoup(response.text,'html.parser') items = soup.select(".article-list .article-item-box")ifnot items:breakfor item in items: title_elem = item.select_one("h4 a")ifnot title_elem:continue title = title_elem.text.strip() link = title_elem["href"] articles.append({"title": title,"url": link}) page +=1 time.sleep(1)return articles defdownload_image(img_url, save_path: Path):"""下载单张图片到本地"""try: img_headers = HEADERS.copy() img_headers["Accept"]="image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8" response = requests.get(img_url, headers=img_headers, stream=True, timeout=30)if response.status_code ==200: save_path.parent.mkdir(parents=True, exist_ok=True)withopen(save_path,'wb')as f:for chunk in response.iter_content(chunk_size=8192):if chunk: f.write(chunk)returnTrueelse:print(f"图片下载失败(状态码:{response.status_code}):{img_url}")returnFalseexcept Exception as e:print(f"图片下载异常:{img_url},错误:{str(e)}")returnFalsedefget_image_extension(img_url):"""从URL中获取图片扩展名""" parsed_url = urlparse(img_url) path = parsed_url.path.lower() extensions =['.jpg','.jpeg','.png','.gif','.webp','.bmp','.svg']for ext in extensions:if ext in path:return ext return'.jpg'defprocess_images_in_content(content, article_title):"""处理内容中的图片,下载并替换为本地路径""" soup = BeautifulSoup(content,'html.parser') img_tags = soup.find_all('img')ifnot img_tags:return content # 清理文章标题用于路径 safe_title = sanitize_filename(article_title) global_image_dir = SAVE_DIR /"images" article_image_dir = global_image_dir / safe_title for img in img_tags: img_url = img.get('src','')ifnot img_url:continue# 处理协议相对路径if img_url.startswith('//'): img_url ='https:'+ img_url elifnot img_url.startswith(('http://','https://')):continue# 跳过无法处理的相对路径try: img_hash = hashlib.md5(img_url.encode()).hexdigest()[:8] img_ext = get_image_extension(img_url) img_filename =f"{img_hash}{img_ext}" local_img_path = article_image_dir / img_filename # Markdown 中使用正斜杠(/),兼容所有平台 md_img_path =f"./images/{safe_title}/{img_filename}"ifnot local_img_path.exists():print(f" 下载图片:{img_filename}")if download_image(img_url, local_img_path): img['src']= md_img_path else:print(f" 图片下载失败,保留原链接:{img_url}")else: img['src']= md_img_path except Exception as e:print(f" 处理图片时出错:{img_url},错误:{str(e)}")continuereturnstr(soup)defdownload_article(url, article_title):"""下载单篇文章,处理图片后转为Markdown"""try: response = requests.get(url, headers=HEADERS, timeout=30) soup = BeautifulSoup(response.text,'html.parser') content = soup.select_one("article")ifnot content:print(f" 未找到文章内容")returnNone processed_content = process_images_in_content(str(content), article_title) markdown_content = md(processed_content)return markdown_content except Exception as e:print(f" 下载文章时出错:{str(e)}")returnNonedefsave_to_markdown(title, content, save_dir: Path):"""保存Markdown文件""" save_dir.mkdir(parents=True, exist_ok=True) safe_title = sanitize_filename(title) filename = save_dir /f"{safe_title}.md"withopen(filename,"w", encoding="utf-8")as f: f.write(f"# {title}\n\n") f.write(content)print(f" 已保存:{filename}")return filename if __name__ =="__main__":print("开始获取文章列表...") articles = get_article_list(ZEEKLOG_USERNAME)print(f"找到 {len(articles)} 篇文章") success_count =0 fail_count =0for i, article inenumerate(articles,1): title = article["title"] url = article["url"]print(f"\n[{i}/{len(articles)}] 处理文章:{title}") content = download_article(url, title)if content: save_to_markdown(title, content, SAVE_DIR) success_count +=1else:print(f" 文章下载失败:{title}") fail_count +=1 time.sleep(2)print(f"\n处理完成!成功:{success_count}篇,失败:{fail_count}篇")print(f"文章保存在:{SAVE_DIR.resolve()}")print("图片保存在:./images/ 目录下,Markdown文件可离线查看")

其中,脚本里ZEEKLOG_USERNAME的值,改为你要获取的ZEEKLOG的用户名

获取用户名:点击作者头像后,链接里的这个值就是用户名(红框里的内容)

在这里插入图片描述

执行脚本

python ZEEKLOG_downloader.py 

执行日志入下:

在这里插入图片描述

由于要下载ZEEKLOG文章里的图片,所以很慢,静静等待即可。下载过程中,会在脚本所在目录生成一个ZEEKLOG_articles文件夹,里边是md文件以及存md里的图片的文件夹。

在这里插入图片描述

二 导出指定日期后的文章

上边的脚本,一次性导出了所有已发布的文章,但是有时候我们的文章太多,每次备份不需要全部导出,只导出指定时间以后的文章,那么使用如下脚本即可,如脚本名为new.py,内容如下

# -*- coding: utf-8 -*-import os import re import requests import time from bs4 import BeautifulSoup from markdownify import markdownify as md from urllib.parse import urlparse, unquote import hashlib from pathlib import Path from datetime import datetime # ================== 配置区 ================== ZEEKLOG_USERNAME ="qq_33417321"# ←←← 修改为你想下载的用户名 SAVE_DIR = Path("ZEEKLOG_articles")# 文章保存根目录(自动跨平台)# ⬇️ 新增:设置最小发布日期(含) MIN_PUBLISH_DATE = datetime(2026,2,7)# 只下载 2026年2月1日及之后的文章 HEADERS ={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36","Referer":"https://blog.ZEEKLOG.net/"}defsanitize_filename(name:str)->str:"""清理文件名,移除 Windows 非法字符和‘原创’字样""" name = name.replace("原创","").strip() name = re.sub(r'[\\/*?:"<>|\r\n]',"_", name)return name or"untitled"defparse_publish_date(date_str:str)-> datetime |None:"""尝试解析 ZEEKLOG 日期字符串,支持 '2025-06-15 10:30:00' 或 '2025-06-15' 等""" date_str = date_str.strip()for fmt in["%Y-%m-%d %H:%M:%S",# 带秒,如 2025-06-15 10:30:09"%Y-%m-%d %H:%M",# 不带秒,如 2025-06-15 10:30"%Y-%m-%d"# 只有日期,如 2025-06-15]:try:return datetime.strptime(date_str, fmt)except ValueError:continueprint(f" 无法解析日期:{date_str}")returnNonedefget_article_list(username, min_date=None):"""获取博主文章列表(标题、URL、发布时间),可选按 min_date 过滤""" url =f"https://blog.ZEEKLOG.net/{username}/article/list" articles =[] page =1 early_stop =Falsewhilenot early_stop:print(f" 正在抓取第 {page} 页...") response = requests.get(f"{url}/{page}", headers=HEADERS) soup = BeautifulSoup(response.text,'html.parser') items = soup.select(".article-list .article-item-box")ifnot items:break current_page_has_valid =False# 当前页是否有满足条件的文章for item in items: title_elem = item.select_one("h4 a") date_elem = item.select_one(".date")# ZEEKLOG 通常用 .date 类表示发布时间ifnot title_elem:continue title = title_elem.text.strip() link = title_elem["href"] pub_date =Noneif date_elem: raw_date = date_elem.text.strip() pub_date = parse_publish_date(raw_date)# 如果设置了最小日期,且文章发布时间早于该日期,则跳过if min_date and pub_date and pub_date < min_date:continue# 如果发布时间未知但设置了 min_date,保守起见也跳过(或可选择保留)if min_date andnot pub_date:print(f" 警告:无法获取文章 [{title}] 的发布时间,跳过(因设置了日期过滤)")continue articles.append({"title": title,"url": link,"publish_date": pub_date }) current_page_has_valid =True# 如果当前页没有任何有效文章(全部早于 min_date),可提前终止if min_date andnot current_page_has_valid and page >1:print(" 后续页面文章均早于指定日期,停止翻页。") early_stop =True page +=1 time.sleep(1)return articles # ========== 以下函数保持不变 ==========defdownload_image(img_url, save_path: Path):try: img_headers = HEADERS.copy() img_headers["Accept"]="image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8" response = requests.get(img_url, headers=img_headers, stream=True, timeout=30)if response.status_code ==200: save_path.parent.mkdir(parents=True, exist_ok=True)withopen(save_path,'wb')as f:for chunk in response.iter_content(chunk_size=8192):if chunk: f.write(chunk)returnTrueelse:print(f"图片下载失败(状态码:{response.status_code}):{img_url}")returnFalseexcept Exception as e:print(f"图片下载异常:{img_url},错误:{str(e)}")returnFalsedefget_image_extension(img_url): parsed_url = urlparse(img_url) path = parsed_url.path.lower() extensions =['.jpg','.jpeg','.png','.gif','.webp','.bmp','.svg']for ext in extensions:if ext in path:return ext return'.jpg'defprocess_images_in_content(content, article_title): soup = BeautifulSoup(content,'html.parser') img_tags = soup.find_all('img')ifnot img_tags:return content safe_title = sanitize_filename(article_title) global_image_dir = SAVE_DIR /"images" article_image_dir = global_image_dir / safe_title for img in img_tags: img_url = img.get('src','')ifnot img_url:continueif img_url.startswith('//'): img_url ='https:'+ img_url elifnot img_url.startswith(('http://','https://')):continuetry: img_hash = hashlib.md5(img_url.encode()).hexdigest()[:8] img_ext = get_image_extension(img_url) img_filename =f"{img_hash}{img_ext}" local_img_path = article_image_dir / img_filename md_img_path =f"./images/{safe_title}/{img_filename}"ifnot local_img_path.exists():print(f" 下载图片:{img_filename}")if download_image(img_url, local_img_path): img['src']= md_img_path else:print(f" 图片下载失败,保留原链接:{img_url}")else: img['src']= md_img_path except Exception as e:print(f" 处理图片时出错:{img_url},错误:{str(e)}")continuereturnstr(soup)defdownload_article(url, article_title):try: response = requests.get(url, headers=HEADERS, timeout=30) soup = BeautifulSoup(response.text,'html.parser') content = soup.select_one("article")ifnot content:print(f" 未找到文章内容")returnNone processed_content = process_images_in_content(str(content), article_title) markdown_content = md(processed_content)return markdown_content except Exception as e:print(f" 下载文章时出错:{str(e)}")returnNonedefsave_to_markdown(title, content, save_dir: Path): save_dir.mkdir(parents=True, exist_ok=True) safe_title = sanitize_filename(title) filename = save_dir /f"{safe_title}.md"withopen(filename,"w", encoding="utf-8")as f: f.write(f"# {title}\n\n") f.write(content)print(f" 已保存:{filename}")return filename # ========== 主程序入口 ==========if __name__ =="__main__":print("开始获取文章列表...") articles = get_article_list(ZEEKLOG_USERNAME, min_date=MIN_PUBLISH_DATE)print(f"找到 {len(articles)} 篇符合条件的文章(发布日期 ≥ {MIN_PUBLISH_DATE.strftime('%Y-%m-%d')})") success_count =0 fail_count =0for i, article inenumerate(articles,1): title = article["title"] url = article["url"] pub_date = article.get("publish_date") date_str = pub_date.strftime("%Y-%m-%d")if pub_date else"未知"print(f"\n[{i}/{len(articles)}] 处理文章:{title} (发布于 {date_str})") content = download_article(url, title)if content: save_to_markdown(title, content, SAVE_DIR) success_count +=1else:print(f" 文章下载失败:{title}") fail_count +=1 time.sleep(2)print(f"\n处理完成!成功:{success_count}篇,失败:{fail_count}篇")print(f"文章保存在:{SAVE_DIR.resolve()}")print("图片保存在:./images/ 目录下,Markdown文件可离线查看")

记得修改MIN_PUBLISH_DATE 里的开始日期。之后执行脚本后,下载下来就是指定日期后的文章了。

Read more

动态规划 路径类 DP 入门:3 道经典例题(最小路径和 + 迷雾森林 + 过河卒)全解析

动态规划 路径类 DP 入门:3 道经典例题(最小路径和 + 迷雾森林 + 过河卒)全解析

文章目录 * 矩阵的最小路径和 * 迷雾森林 * 过河卒 路径类 dp 是线性 dp 的⼀种,它是在⼀个 n × m 的矩阵中设置⼀个⾏⾛规则,研究从起点⾛到终点的 ⽅案数、最⼩路径和或者最⼤路径和等等的问题。 ⼊⻔阶段的《数字三⻆形》其实就是路径类 dp。 矩阵的最小路径和 题目描述 题目解析 1、状态表示 dp[i][j]表示从[1 1]格子走到[i j]格子时,所有方案下的最小路径和。 2、状态转移方程 我们还是以最后一步来推导状态转移方程,走到最后一个格子dp[n][m]

By Ne0inhk

优选算法——前缀和

👇作者其它专栏 《数据结构与算法》《算法》《C++起始之路》 前缀和相关题解 1.前缀和 算法思路: a.先预处理出来一个【前缀和】数组:         用dp[i]表示:[1,i]区间内所有元素的和,那么dp[i-1]里面存的就是[1,i-1]区间内所有元素的和,那么:可得到递推公式:dp[i]=dp[i-1]+arr[i]; b.使用前缀和数组,【快速】求出【某一个区间内】所有元素的和:         当访问的区间是[l,r]时:区间内所有元素的和为:dp[r]-dp[l-r]。 #include <

By Ne0inhk
python鼻炎在线预约挂号评价系统微信小程序的设计与开发论文_76g3o--(flask django Pycharm)

python鼻炎在线预约挂号评价系统微信小程序的设计与开发论文_76g3o--(flask django Pycharm)

目录 * 摘要 * 关于博主 * 开发技术路线 * 相关技术介绍 * 核心代码参考示例 * 结论 * 源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! 摘要 该论文围绕基于Python的“鼻护灵”鼻炎在线预约挂号评价系统微信小程序的设计与开发展开。系统采用Flask和Django框架作为后端开发工具,结合Pycharm集成开发环境,实现了鼻炎专科医院的在线预约、挂号、评价及管理功能。 系统前端通过微信小程序提供用户友好的交互界面,支持患者在线选择医生、预约时间段、查看就诊记录及提交服务评价。后端采用Flask轻量级框架处理业务逻辑,Django框架用于数据管理,确保系统的高效性和可扩展性。数据库采用MySQL,存储患者信息、医生排班、预约记录及评价数据。 系统设计遵循模块化原则,包含用户管理、预约挂号、评价反馈、后台管理四大核心模块。通过RESTful API实现前后端数据交互,保障系统安全性与稳定性。测试结果表明,系统运行流畅,能够有效提升鼻炎患者的就诊效率与医疗服务体验,为医疗信息化提供了可行方案。 关键词:Python;微信

By Ne0inhk

4步实现Python版本自由:pyenv多环境管理完全指南

4步实现Python版本自由:pyenv多环境管理完全指南 【免费下载链接】pyenvSimple Python version management 项目地址: https://gitcode.com/GitHub_Trending/py/pyenv 在Python开发中,环境配置与多版本切换是开发者绕不开的挑战。不同项目可能依赖特定Python版本,系统自带版本与项目需求冲突、全局包污染等问题常常导致开发效率低下。pyenv作为轻量级版本管理工具,通过垫片机制实现版本隔离,让开发者在不同Python环境间无缝切换,彻底解决版本冲突难题。本文将从环境部署到高级应用,全面讲解pyenv的实战技巧,帮助开发者构建高效可控的Python开发环境。 环境部署:3分钟极速安装 仓库克隆与基础配置 pyenv采用源码安装方式,通过以下命令克隆官方仓库并配置环境变量: # 克隆pyenv核心仓库 git clone https://gitcode.com/GitHub_Trending/py/pyenv ~/.pyenv # 配置环境变量(以bash为例) echo 'export

By Ne0inhk