Python脚本每天爬取微博热搜-升级版

在这里插入图片描述

主要优化内容:

定时任务调整:

将定时任务从每小时改为每10分钟执行一次

调整了请求延迟时间,从1-3秒减少到0.5-1.5秒

缩短了请求超时时间,从10秒减少到8秒

性能优化:

移除了广告数据的处理,减少不必要的处理

优化了数据结构,减少内存占用

添加了数据清理功能,自动删除7天前的数据

用户体验改进:

HTML页面添加了自动刷新功能(每5分钟)

添加了手动刷新按钮

增加了统计信息显示(总热搜数、时间段数等)

优化了移动端显示效果

代码健壮性:

添加了异常处理

优化了数据存储和读取逻辑

添加了数据清理机制,防止数据无限增长

界面美化:

添加了网站图标

优化了颜色方案和布局

改进了响应式设计,适配移动设备

这个优化版本每10分钟获取一次微博热搜,并将新数据追加到当天的HTML文件中,同时保持了去重功能。页面也会每5分钟自动刷新,确保用户看到最新数据。

import requests
from bs4 import BeautifulSoup
import time
import os
from datetime import datetime, timedelta
import schedule
import random
import json
import redef fetch_weibo_hot():"""使用API接口获取微博热搜数据(避免HTML结构变化问题)"""api_url = "https://weibo.com/ajax/side/hotSearch"headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36','Referer': 'https://weibo.com/','Cookie': 'XSRF-TOKEN=RKnNBEaQBrp=='  # 替换为你的实际Cookie}try:# 添加随机延迟避免被封time.sleep(random.uniform(0.5, 1.5))response = requests.get(api_url, headers=headers, timeout=8)response.raise_for_status()# 解析JSON数据data = response.json()# 提取热搜数据hot_items = []for group in data['data']['realtime']:# 普通热搜项if 'word' in group:item = {'rank': group.get('rank', ''),'title': group['word'],'hot': group.get('num', '0'),'link': f"https://s.weibo.com/weibo?q={group['word']}",'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),'source': 'weibo'}hot_items.append(item)# 只保留前50条热搜return hot_items[:50]except Exception as e:print(f"🚨 获取热搜数据失败: {e}")return []def load_existing_hot_searches(date_str):"""加载当天已存在的热搜数据"""json_file = os.path.join("weibo_hot", date_str, "hot_searches.json")if os.path.exists(json_file):try:with open(json_file, 'r', encoding='utf-8') as f:return json.load(f)except Exception as e:print(f"读取已有热搜数据失败: {e}")return []return []def save_hot_data_json(hot_data, date_str):"""保存热搜数据到JSON文件"""daily_dir = os.path.join("weibo_hot", date_str)if not os.path.exists(daily_dir):os.makedirs(daily_dir)json_file = os.path.join(daily_dir, "hot_searches.json")# 加载已有数据existing_data = load_existing_hot_searches(date_str)existing_titles = {item['title'] for item in existing_data}# 过滤掉已存在的热搜new_data = [item for item in hot_data if item['title'] not in existing_titles]# 合并数据all_data = existing_data + new_data# 保存到JSON文件with open(json_file, 'w', encoding='utf-8') as f:json.dump(all_data, f, ensure_ascii=False, indent=2)return all_data, len(new_data)def generate_html(hot_data, date_str):"""生成HTML文件"""if not hot_data:return "<html><body><h1>未获取到热搜数据</h1></body></html>"# 按时间分组热搜time_groups = {}for item in hot_data:time_key = item['timestamp'][:13]  # 只取到小时if time_key not in time_groups:time_groups[time_key] = []time_groups[time_key].append(item)# 按时间倒序排列sorted_times = sorted(time_groups.keys(), reverse=True)# 统计信息total_count = len(hot_data)time_count = len(time_groups)html_content = f"""<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>微博热搜榜 {date_str}</title><link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🔥</text></svg>"><style>* {{box-sizing: border-box;margin: 0;padding: 0;}}body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif; background-color: #f5f8fa;color: #14171a;line-height: 1.5;padding: 20px;max-width: 1200px;margin: 0 auto;}}.container {{background: white;border-radius: 16px;box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);overflow: hidden;margin-bottom: 30px;}}.header {{ background: linear-gradient(135deg, #ff6b6b, #5e72eb);color: white;padding: 25px 30px;position: relative;}}.title {{font-size: 28px;font-weight: 700;margin-bottom: 5px;}}.subtitle {{font-size: 16px;opacity: 0.9;}}.stats {{margin-top: 15px;display: flex;gap: 15px;flex-wrap: wrap;}}.stat-item {{background: rgba(255, 255, 255, 0.2);padding: 8px 15px;border-radius: 20px;font-size: 14px;}}.update-time {{position: absolute;top: 25px;right: 30px;background: rgba(0, 0, 0, 0.15);padding: 5px 12px;border-radius: 20px;font-size: 14px;}}.time-section {{margin: 20px 0;padding: 15px;background: #f8f9fa;border-radius: 8px;}}.time-header {{font-size: 18px;font-weight: 600;margin-bottom: 15px;color: #495057;display: flex;align-items: center;justify-content: space-between;}}.time-info {{display: flex;align-items: center;}}.time-count {{background: #e9ecef;padding: 2px 8px;border-radius: 10px;font-size: 14px;margin-left: 10px;}}.hot-list {{padding: 0;}}.hot-item {{display: flex;align-items: center;padding: 16px 20px;border-bottom: 1px solid #e6ecf0;transition: background 0.2s;}}.hot-item:hover {{background-color: #f7f9fa;}}.rank {{width: 36px;height: 36px;line-height: 36px;text-align: center;font-weight: bold;font-size: 16px;background: #f0f2f5;border-radius: 8px;margin-right: 15px;flex-shrink: 0;}}.top1 {{ background: linear-gradient(135deg, #ff9a9e, #fad0c4);color: #d63031;}}.top2 {{ background: linear-gradient(135deg, #a1c4fd, #c2e9fb);color: #0984e3;}}.top3 {{ background: linear-gradient(135deg, #ffecd2, #fcb69f);color: #e17055;}}.hot-content {{flex: 1;min-width: 0;}}.hot-title {{font-size: 17px;font-weight: 500;margin-bottom: 6px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}}.hot-stats {{display: flex;align-items: center;color: #657786;font-size: 14px;gap: 15px;flex-wrap: wrap;}}.hot-value {{color: #ff6b6b;font-weight: 700;}}.hot-time {{font-size: 12px;color: #868e96;}}.link {{color: #1da1f2;text-decoration: none;transition: color 0.2s;}}.link:hover {{color: #0d8bda;text-decoration: underline;}}.footer {{text-align: center;padding: 20px;color: #657786;font-size: 13px;border-top: 1px solid #e6ecf0;}}.no-data {{text-align: center;padding: 40px;color: #657786;}}.collapse-btn {{background: #6c757d;color: white;border: none;padding: 5px 10px;border-radius: 4px;cursor: pointer;font-size: 12px;}}.collapse-btn:hover {{background: #5a6268;}}.collapsed .hot-list {{display: none;}}.auto-refresh {{text-align: center;margin: 20px 0;}}.refresh-btn {{background: #28a745;color: white;border: none;padding: 10px 20px;border-radius: 5px;cursor: pointer;font-size: 14px;}}.refresh-btn:hover {{background: #218838;}}@media (max-width: 768px) {{body {{padding: 10px;}}.header {{padding: 20px 15px;}}.title {{font-size: 22px;}}.update-time {{position: static;margin-top: 10px;}}.hot-item {{padding: 14px 15px;}}.hot-title {{font-size: 16px;}}.hot-stats {{flex-direction: column;align-items: flex-start;gap: 5px;}}}}</style></head><body><div class="container"><div class="header"><h1 class="title">微博热搜榜</h1><div class="subtitle">全天热点汇总 · 每10分钟更新</div><div class="stats"><div class="stat-item">📊 总热搜数: {total_count}</div><div class="stat-item">🕒 时间段: {time_count}</div><div class="stat-item">⏰ 更新频率: 每10分钟</div></div><div class="update-time">最后更新: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</div></div>"""# 添加时间分组的热搜内容for time_key in sorted_times:time_display = f"{time_key}:00"time_data = time_groups[time_key]html_content += f"""<div class="time-section"><div class="time-header"><div class="time-info">🕒 {time_display}<span class="time-count">{len(time_data)} 条热搜</span></div><button class="collapse-btn" onclick="this.parentElement.parentElement.classList.toggle('collapsed')">折叠/展开</button></div><div class="hot-list">"""for item in time_data:rank_class = ""if item['rank'] == 1:rank_class = "top1"elif item['rank'] == 2:rank_class = "top2"elif item['rank'] == 3:rank_class = "top3"hot_value = f"<span class='hot-value'>🔥 {item['hot']}</span>" if item['hot'] else ""html_content += f"""<div class="hot-item"><div class="rank {rank_class}">{item['rank']}</div><div class="hot-content"><div class="hot-title"><a href="{item['link']}" class="link" target="_blank">{item['title']}</a></div><div class="hot-stats">{hot_value}<span class="hot-time">📅 {item['timestamp']}</span></div></div></div>"""html_content += """</div></div>"""if not hot_data:html_content += """<div class="no-data"><h3>暂无热搜数据</h3><p>请检查网络连接或稍后再试</p></div>"""html_content += f"""<div class="auto-refresh"><button class="refresh-btn" onclick="location.reload()">🔄 刷新页面</button><p>页面每10分钟自动更新,也可手动刷新</p></div><div class="footer">数据来源: 微博热搜 • 每10分钟自动更新 • 最后更新: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} • 仅供学习参考</div></div><script>// 默认折叠所有时间段的搜索结果document.addEventListener('DOMContentLoaded', function() {{var sections = document.querySelectorAll('.time-section');sections.forEach(function(section, index) {{if (index > 0) {{ // 保持最新时间段展开section.classList.add('collapsed');}}}});// 设置自动刷新(每5分钟)setTimeout(function() {{location.reload();}}, 5 * 60 * 1000);}});</script></body></html>"""return html_contentdef cleanup_old_data():"""清理7天前的数据"""try:now = datetime.now()cutoff_date = (now - timedelta(days=7)).strftime("%Y-%m-%d")weibo_hot_dir = "weibo_hot"if os.path.exists(weibo_hot_dir):for date_dir in os.listdir(weibo_hot_dir):if date_dir < cutoff_date:dir_path = os.path.join(weibo_hot_dir, date_dir)import shutilshutil.rmtree(dir_path)print(f"🗑️ 已清理过期数据: {date_dir}")except Exception as e:print(f"清理旧数据时出错: {e}")def save_hot_data():"""保存热搜数据"""try:# 创建存储目录if not os.path.exists("weibo_hot"):os.makedirs("weibo_hot")# 获取当前日期now = datetime.now()date_str = now.strftime("%Y-%m-%d")print(f"🕒 开始获取 {now.strftime('%Y-%m-%d %H:%M:%S')} 的热搜数据...")hot_data = fetch_weibo_hot()if hot_data:print(f"✅ 成功获取 {len(hot_data)} 条热搜数据")# 保存到JSON文件并获取所有数据all_data, new_count = save_hot_data_json(hot_data, date_str)print(f"📊 已有 {len(all_data)} 条热搜,新增 {new_count} 条")# 生成HTML内容html_content = generate_html(all_data, date_str)# 保存HTML文件html_file = os.path.join("weibo_hot", date_str, "index.html")with open(html_file, "w", encoding="utf-8") as f:f.write(html_content)print(f"💾 已保存到: {html_file}")# 每周清理一次旧数据#if now.weekday() == 0 and now.hour == 0 and now.minute < 10:  # 每周一凌晨#    cleanup_old_data()else:print("⚠️ 未获取到热搜数据,跳过保存")except Exception as e:print(f"❌ 保存数据时出错: {e}")def job():"""定时任务"""print("\n" + "="*60)print(f"⏰ 执行定时任务: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")save_hot_data()print("="*60 + "\n")if __name__ == "__main__":print("🔥 微博热搜爬虫已启动 🔥")print("⏳ 首次执行将立即运行,之后每10分钟执行一次")print("💡 提示: 请确保已更新有效的Cookie")print("="*60)# 立即执行一次job()# 设置定时任务(每10分钟执行一次)schedule.every(10).minutes.do(job)# 每天凌晨清理一次旧数据schedule.every().day.at("00:00").do(cleanup_old_data)print("⏳ 程序运行中,按Ctrl+C退出...")try:# 保持程序运行while True:schedule.run_pending()time.sleep(30)  # 每30秒检查一次except KeyboardInterrupt:print("\n👋 程序已手动停止")

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/96234.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/96234.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

win11兼容运行远古游戏

游戏<远古战争>属于win7时代的游戏&#xff0c;在win11系统中运行&#xff0c;当鼠标移动立马卡住 解决方案&#xff1a; 最优&#xff1a;采用wmware虚拟机安装win7系统 最简单&#xff1a;使用 DxWnd 模拟老游戏运行环境 DxWnd官网下载 附录&#xff1a;游戏下载网址…

Docker小游戏 | 使用Docker部署人生重开模拟器

Docker小游戏 | 使用Docker部署人生重开模拟器 前言 项目介绍 项目简介 项目预览 二、系统要求 环境要求 环境检查 Docker版本检查 检查操作系统版本 三、部署人生重开模拟器小游戏 下载镜像 创建容器 检查容器状态 检查服务端口 安全设置 四、访问人生重开模拟器 五、总结 前言…

从依赖到自研:一个客服系统NLP能力的跃迁之路

前言&#xff1a;七年磨一剑的技术突围2015年在某平台上线初期&#xff0c;智能客服系统即采用行业通用的第三方NLP解决方案。在随后的八年发展历程中&#xff0c;系统虽历经三次重大版本迭代&#xff0c;但始终未能突破核心语义识别能力的外部依赖。这种依赖带来了三重困境&am…

50.Seata-AT模式

AT模式同样是分阶段提交的事务模型。优势是弥补了XA模型中资源锁定周期过长的缺陷。 没有代码入侵,框架自动完成快照生成、回滚和提交。实现非常简单。 两阶段之间属于软状态,属于最终一致。 AT模式 阶段一RM的工作: 1.注册分支事务 2.记录undo-log (数据快照),记录更…

Android13车机系统自定义系统栏显示策略之状态栏下拉异常

1、引言 文章《Android13车机系统实现系统栏自定义显示策略》介绍了车机系统上自定义系统栏(状态栏、底部栏)显示策略,文中末尾提到了一个遗留问题: 由于状态栏区域支持点击或下拉显示出快捷设置&消息通知栏,三方应用显示时,从状态栏中间区域而不从顶部边缘下拉,底…

【Langchain系列五】DbGPT——Langchain+PG构建结构化数据库智能问答系统

Langchain二次开发专栏 【Langchain系列一】常用大模型的key获取与连接方式 【Langchain系列二】LangChain+Prompt +LLM智能问答入门 【Langchain系列三】GraphGPT——LangChain+NebulaGraph+llm构建智能图数据库问答系统 【Langchain系列四】RAG——基于非结构化数据库的智能问…

生信分析自学攻略 | R语言数据类型和数据结构

在前面两篇文章中&#xff0c;我们已经成功搭建了R和RStudio这一强大的生信分析平台。然而&#xff0c;工具再好&#xff0c;若不懂得如何“放置”和“理解”你的数据&#xff0c;一切都将寸步难行。今天&#xff0c;我们将学习R语言最重要的部分——数据类型&#xff08;Data …

Python工程与模块命名规范:构建可维护的大型项目架构

目录 Python工程与模块命名规范&#xff1a;构建可维护的大型项目架构 引言&#xff1a;命名的重要性 在软件开发中&#xff0c;命名可能是最容易被忽视但却是最重要的实践之一。根据2023年对Python开源项目的分析&#xff0c;超过35%的维护问题与糟糕的命名约定直接相关。一个…

Props 与 State 类型定义

下面&#xff0c;我们来系统的梳理关于 TypeScript 集成&#xff1a;Props 与 State 类型定义 的基本知识点&#xff1a;一、TypeScript 在 React 中的核心价值 TypeScript 为 React 开发提供了强大的类型安全保证&#xff0c;特别是在定义组件 Props 和 State 时&#xff1a; …

[1Prompt1Story] 注意力机制增强 IPCA | 去噪神经网络 UNet | U型架构分步去噪

第五章&#xff1a;注意力机制增强&#xff08;IPCA&#xff09; 欢迎回到1Prompt1Story&#x1f43b;‍❄️ 在第四章中&#xff0c;我们掌握了**语义向量重加权&#xff08;SVR&#xff09;**技术&#xff0c;通过语义向量调节实现核心要素强化。 但当场景从"雪地嬉戏…

【P7071 [CSP-J2020] 优秀的拆分 - 洛谷 https://www.luogu.com.cn/problem/P7071】

题目 P7071 [CSP-J2020] 优秀的拆分 - 洛谷 https://www.luogu.com.cn/problem/P7071 代码 #include <bits/stdc.h> using namespace std; const int N1e71; int d; vector<int> v; bool k[N]; bool fen(int x){if(x0)return 1;//能拆分完 for(int ix;i>x/…

从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践

了解更多&#xff0c;搜索"程序员老狼"作为一名Golang开发者&#xff0c;我最近在维护一个客服系统时遇到了一个看似简单却值得深思的问题&#xff1a;如何将项目中遗留的ioutil.ReadFile调用迁移到现代的os.ReadFile。这看似只是一个简单的函数替换&#xff0c;但背…

Python UI自动化测试Web frame及多窗口切换

这篇文章主要为大家介绍了Python UI自动化测试Web frame及多窗口切换&#xff0c;有需要的朋友可以借鉴参考下&#xff0c;希望能够有所帮助&#xff0c;祝大家多多进步&#xff0c;早日升职加薪 一、什么是frame&frame切换&#xff1f; frame&#xff1a;HTML页面中的一…

工业相机基本知识解读:像元、帧率、数据接口等

工业相机&#xff08;Industrial Camera&#xff09;是一种专门为工业自动化和机器视觉应用而设计的成像设备&#xff0c;它不同于消费类相机&#xff08;如手机、单反&#xff09;&#xff0c;主要追求的是成像稳定性、长时间可靠性、实时性和精确性。它通常与镜头、光源、图像…

RTC之神奇小闹钟

&#x1f3aa; RTC 是什么&#xff1f;—— 电子设备的“迷你生物钟”想象一下&#xff1a;你晚上睡觉时&#xff0c;手机关机了。但当你第二天开机&#xff0c;它居然知道现在几点&#xff01;这就是 RTC&#xff08;Real-Time Clock&#xff0c;实时时钟&#xff09; 的功劳&…

判断IP是否属于某个网段

判断IP是否属于某个网段判断一个IP是否是否属于某个CIDR网段&#xff0c;核心是比较IP与网段的网络位是否一致&#xff0c;步骤如下&#xff1a; 一、明确CIDR网段的两个关键信息 假设要判断的IP是 IPx&#xff0c;目标网段是 CIDR 网段地址/n&#xff08;例如 192.168.1.0/24…

Python day50

浙大疏锦行 python day50. 在预训练模型&#xff08;resnet18&#xff09;中添加cbam注意力机制&#xff0c;需要修改模型的架构&#xff0c;同时应该考虑插入的cbam注意力机制模块的位置&#xff1b; import torch import torch.nn as nn from torchvision import models# 自…

VPS海外节点性能监控全攻略:从基础配置到高级优化

在全球化业务部署中&#xff0c;VPS海外节点的稳定运行直接影响用户体验。本文将深入解析如何构建高效的性能监控体系&#xff0c;涵盖网络延迟检测、资源阈值设置、告警机制优化等核心环节&#xff0c;帮助运维人员实现跨国服务器的可视化管控。 VPS海外节点性能监控全攻略&am…

C语言初学者笔记【结构体】

文章目录一、结构体的使用1. 结构体声明2. 变量创建与初始化3. 特殊声明与陷阱二、内存对齐1. 规则&#xff1a;2. 示例分析&#xff1a;3. 修改默认对齐数&#xff1a;三、结构体传参四、结构体实现位段1. 定义2. 内存分配3. 应用场景4. 跨平台问题&#xff1a;5. 注意事项&am…

基于XGBoost算法的数据回归预测 极限梯度提升算法 XGBoost

一、作品详细简介 1.1附件文件夹程序代码截图 全部完整源代码&#xff0c;请在个人首页置顶文章查看&#xff1a; 学行库小秘_CSDN博客​编辑https://blog.csdn.net/weixin_47760707?spm1000.2115.3001.5343 1.2各文件夹说明 1.2.1 main.m主函数文件 该MATLAB 代码实现了…