RSS动态内容过滤的核心逻辑
RSS(简易信息聚合)是一种基于XML格式的内容分发协议,每个RSS源都会定期更新包含标题、链接、描述、发布时间等字段的内容项。动态内容过滤的本质是在获取RSS源数据后,按照预设的规则对内容项进行筛选,只保留符合要求的条目,过滤掉无关内容。

RSS源的基本结构
标准的RSS 2.0格式内容结构如下,我们需要先解析这些字段才能进行过滤:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>示例RSS源</title>
<link>https://ipipp.com/rss</link>
<description>这是一个示例RSS订阅源</description>
<item>
<title>第一条内容标题</title>
<link>https://ipipp.com/post/1</link>
<description>第一条内容的详细描述信息</description>
<pubDate>Mon, 01 Jan 2024 08:00:00 GMT</pubDate>
</item>
<item>
<title>第二条内容标题</title>
<link>https://ipipp.com/post/2</link>
<description>第二条内容的详细描述信息</description>
<pubDate>Mon, 01 Jan 2024 09:00:00 GMT</pubDate>
</item>
</channel>
</rss>
动态过滤的常见规则类型
实际使用中,动态过滤通常支持以下几类规则,用户可以根据需求组合配置:
- 关键词过滤:匹配标题或描述中包含指定关键词的内容,支持正向匹配和反向匹配
- 时间范围过滤:只保留指定时间范围内发布的内容,过滤过期内容
- 来源过滤:根据内容来源链接或作者信息筛选符合要求的内容
- 正则匹配过滤:通过正则表达式匹配更复杂的字段规则,比如匹配特定格式的标题
Python实现RSS动态内容过滤的完整示例
下面使用Python的feedparser库解析RSS源,结合自定义过滤规则实现动态内容过滤,首先安装依赖库:
pip install feedparser
定义过滤规则配置
我们将过滤规则定义为字典结构,方便后续动态修改:
# 过滤规则配置
filter_rules = {
"include_keywords": ["技术", "编程", "开发"], # 标题或描述中包含这些关键词则保留
"exclude_keywords": ["广告", "推广"], # 标题或描述中包含这些关键词则过滤
"time_range": {
"start": "2024-01-01 00:00:00", # 起始时间
"end": "2024-12-31 23:59:59" # 结束时间
},
"title_regex": r"^[技术].*" # 标题以"技术"开头的正则规则
}
实现核心过滤函数
核心函数会先解析RSS源,再逐条按照规则筛选内容:
import feedparser
import re
from datetime import datetime
def filter_rss_content(rss_url, rules):
# 解析RSS源
feed = feedparser.parse(rss_url)
if feed.bozo:
print("RSS源解析失败,错误信息:", feed.bozo_exception)
return []
filtered_items = []
# 时间格式转换函数
def parse_pub_date(date_str):
try:
return datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
except:
return None
# 遍历所有内容项
for item in feed.entries:
title = item.get("title", "")
description = item.get("description", "")
link = item.get("link", "")
pub_date_str = item.get("published", "")
pub_date = parse_pub_date(pub_date_str)
# 1. 反向关键词过滤:包含排除关键词则跳过
exclude_match = False
for kw in rules.get("exclude_keywords", []):
if kw in title or kw in description:
exclude_match = True
break
if exclude_match:
continue
# 2. 正向关键词过滤:不包含正向关键词则跳过
include_match = False
for kw in rules.get("include_keywords", []):
if kw in title or kw in description:
include_match = True
break
if not include_match and rules.get("include_keywords"):
continue
# 3. 时间范围过滤:不在时间范围内则跳过
time_range = rules.get("time_range")
if time_range and pub_date:
start_time = datetime.strptime(time_range["start"], "%Y-%m-%d %H:%M:%S")
end_time = datetime.strptime(time_range["end"], "%Y-%m-%d %H:%M:%S")
if not (start_time <= pub_date.replace(tzinfo=None) <= end_time):
continue
# 4. 正则规则过滤:标题不符合正则则跳过
title_regex = rules.get("title_regex")
if title_regex and not re.match(title_regex, title):
continue
# 所有规则都通过,加入结果列表
filtered_items.append({
"title": title,
"link": link,
"description": description,
"pub_date": pub_date_str
})
return filtered_items
调用示例与结果输出
使用上述函数过滤指定的RSS源,这里以示例RSS地址为例:
if __name__ == "__main__":
rss_url = "https://ipipp.com/example_rss.xml"
result = filter_rss_content(rss_url, filter_rules)
print(f"过滤后共得到{len(result)}条内容:")
for idx, item in enumerate(result, 1):
print(f"第{idx}条:")
print(f"标题:{item['title']}")
print(f"链接:{item['link']}")
print(f"发布时间:{item['pub_date']}")
print("-" * 50)
过滤规则的动态更新实现
要实现真正的动态过滤,还需要支持规则的实时更新,不需要修改代码即可调整过滤条件。可以将过滤规则存储在配置文件中,每次过滤前读取最新配置:
import json
def load_filter_rules(config_path):
"""从配置文件加载过滤规则"""
with open(config_path, "r", encoding="utf-8") as f:
return json.load(f)
def dynamic_filter_rss(rss_url, config_path):
"""动态加载规则并过滤RSS内容"""
rules = load_filter_rules(config_path)
return filter_rss_content(rss_url, rules)
配置文件filter_config.json的内容如下:
{
"include_keywords": ["Python", "Java", "前端"],
"exclude_keywords": ["游戏", "娱乐"],
"time_range": {
"start": "2024-03-01 00:00:00",
"end": "2024-03-31 23:59:59"
},
"title_regex": ".*开发.*"
}
注意事项
- 部分RSS源可能有访问频率限制,过滤时建议添加请求间隔,避免被封禁
- 解析RSS时注意处理编码问题,避免中文内容出现乱码
- 正则规则尽量写得简洁,避免复杂正则导致过滤性能下降
- 如果RSS源更新频繁,可以结合定时任务定期执行过滤逻辑,自动推送过滤后的内容