简介:本文从浏览器扩展、CSS注入、用户脚本到网络请求拦截,提供多种屏蔽百度热榜的技术方案,适合开发者及普通用户按需选择。
百度搜索作为国内主流搜索引擎,其首页及搜索结果页默认显示”百度热榜”模块,该功能虽能提供实时热点信息,但对部分用户而言存在以下困扰:
经技术分析,百度热榜模块通过动态JS加载,DOM结构包含特定class标识(如hot-search-wrapper),这为技术屏蔽提供了切入点。
实现原理:通过自定义过滤规则隐藏特定DOM元素
操作步骤:
www.baidu.com###hot-search-wrapper进阶配置:
在扩展的”我的过滤器”中添加:
www.baidu.com##.hot-search-wrapperwww.baidu.com##div[class*="hot-search"]
核心代码示例:
// manifest.json{"manifest_version": 3,"name": "百度热榜屏蔽器","version": "1.0","content_scripts": [{"matches": ["*://*.baidu.com/*"],"js": ["content.js"],"run_at": "document_end"}]}// content.jsfunction removeHotSearch() {const hotSearch = document.querySelector('.hot-search-wrapper');if (hotSearch) hotSearch.remove();// 处理动态加载的情况const observer = new MutationObserver(mutations => {mutations.forEach(mutation => {mutation.addedNodes.forEach(node => {if (node.classList?.contains('hot-search-wrapper')) {node.remove();}});});});observer.observe(document.body, {childList: true,subtree: true});}removeHotSearch();
Chrome/Edge方法:
chrome://settings/appearance
.hot-search-wrapper { display: none !important; }div[id^="hotsearch"] { visibility: hidden !important; }
安装Stylus扩展后,创建新样式:
适用域名:*.baidu.com@namespace url(http://www.w3.org/1999/xhtml);@-moz-document domain("baidu.com") {.s-hotsearch-wrapper,#hotsearch-container {display: none !important;}}
完整脚本示例:
// ==UserScript==// @name 百度热榜屏蔽器// @namespace http://tampermonkey.net/// @version 0.1// @description 屏蔽百度搜索页的热榜模块// @author You// @match *://*.baidu.com/s*// @match *://*.baidu.com/*// @grant none// ==/UserScript==(function() {'use strict';function hideHotSearch() {const styles = document.createElement('style');styles.textContent = `.hot-search-wrapper,#hotsearch-container,[class*="hot-search"] {display: none !important;}`;document.head.appendChild(styles);// 直接移除现有元素document.querySelectorAll('.hot-search-wrapper').forEach(el => el.remove());}// 初始执行hideHotSearch();// 监听动态变化const observer = new MutationObserver(hideHotSearch);observer.observe(document.body, {childList: true,subtree: true});})();
*://*.baidu.com/su*
if (window.location.hostname.includes('baidu.com')) {document.addEventListener('DOMContentLoaded', () => {const hotSearch = document.querySelector('.hot-search-wrapper');if (hotSearch) hotSearch.style.display = 'none';});}
操作步骤:
127.0.0.1 hot.baidu.com
Via浏览器:
baidu.com##.hot-search-wrapper
Kiwi浏览器:
||baidu.com^$domain=~baidu.com|~m.baidu.com,xmlhttprequest,objectbaidu.com##div[class*="hot-search"]
推荐使用SearXNG等开源搜索引擎,或配置Squid代理过滤特定内容。
| 方案类型 | 适用场景 | 持久性 | 技术难度 | 维护成本 |
|---|---|---|---|---|
| 浏览器扩展 | 长期使用,多浏览器 | 高 | 中 | 低 |
| CSS注入 | 简单屏蔽,快速生效 | 中 | 低 | 极低 |
| 用户脚本 | 需要动态处理 | 高 | 中高 | 中 |
| 网络拦截 | 高级用户,精准控制 | 高 | 高 | 高 |
| 移动端方案 | 手机用户 | 中 | 中 | 低 |
推荐组合:
规则失效:百度可能更新DOM结构,需定期检查选择器
div[id*="hot"]动态加载问题:
// 在用户脚本中添加轮询检查setInterval(() => {document.querySelectorAll('.hot-search-wrapper').forEach(el => el.remove());}, 1000);
跨域限制:确保脚本在百度域名下执行,避免使用document.domain修改
性能影响:优化观察器配置:
new MutationObserver(callback).observe(document.body, {childList: true,subtree: true,// 限制观察范围attributes: false});
对于高级用户,可以:
requests库获取纯净搜索结果示例代码(获取百度搜索结果去广告):
import requestsfrom bs4 import BeautifulSoupdef clean_baidu_search(query):url = f"https://www.baidu.com/s?wd={query}"headers = {'User-Agent': 'Mozilla/5.0'}response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, 'html.parser')# 移除热榜等无关模块for element in soup.select('.hot-search-wrapper, .c-gap-bottom'):element.decompose()# 提取有效结果results = []for item in soup.select('#content_left .result'):title = item.find('h3').get_text(strip=True)link = item.find('a')['href']results.append({'title': title, 'link': link})return results
通过以上技术方案,用户可根据自身技术水平和需求,选择最适合的百度热榜屏蔽方法。所有方案均基于公开技术实现,符合网络开发规范。