如何获取排除指定用户的维基百科近期更改ATOM feed并解决Unicode过滤问题
维基百科ATOM feed排除指定用户过滤方案
- 原有命令失效核心原因:1. ATOM feed自带
http://www.w3.org/2005/Atom默认命名空间,无命名空间的xpath匹配规则对Unicode字符解析存在适配问题;2. 匿名编辑条目没有author/name节点,原有shell字段分割逻辑会因为字段缺失错位。
方案一:修正后shell命令(适配Unicode+解决匿名条目异常)
# 替换t1、t2为你要排除的用户名,可按需增删变量 t1='Aditya tamhankar' t2='संतोष गोरे' # 过滤并输出完整entry节点,避免字段拆分异常 ( echo 'setns atom=http://www.w3.org/2005/Atom' echo "cat /atom:feed/atom:entry[not(atom:author/atom:name = '$t1' or atom:author/atom:name = '$t2')]" ) | xmllint --shell out.txt | sed '1,/\/ > /d;$d' > filtered_feed.xml
验证过滤效果命令:
# 检查是否还有要排除的用户 xmllint --xpath "//*[name()='author']/*[name()='name']/text()" filtered_feed.xml | grep -E 'Aditya tamhankar|संतोष गोरे'
方案二:Python脚本方案(最稳定,无编码/命名空间坑)
不需要处理shell转义和编码问题,适配所有字符集用户名:
import xml.etree.ElementTree as ET # 配置:要排除的用户名列表,可随意增删 BLOCKED_USERS = {"Aditya tamhankar", "संतोष गोरे"} # ATOM命名空间 NS = {"atom": "http://www.w3.org/2005/Atom"} # 解析feed文件 tree = ET.parse("out.txt") root = tree.getroot() # 遍历所有entry,删除匹配屏蔽用户的节点 for entry in root.findall("atom:entry", NS): author_elem = entry.find("atom:author/atom:name", NS) # 匿名编辑无作者名,保留;匹配屏蔽列表的删除 if author_elem is not None and author_elem.text in BLOCKED_USERS: root.remove(entry) # 输出过滤后的完整feed tree.write("filtered_feed.xml", encoding="utf-8", xml_declaration=True)
运行方法:保存为filter_feed.py,和out.txt放在同一目录,执行python3 filter_feed.py即可得到过滤后的filtered_feed.xml。
内容的提问来源于stack exchange,提问作者shantanuo
相关产品推荐
相关产品推荐

