Python爬取RSS源本质是获取XML格式HTTP响应,需用requests发送带User-Agent的GET请求、显式设encoding为utf-8、检查status_code和Content-Type,再用xml.etree.ElementTree解析rss或feed根节点及item/entry条目,注意命名空间、HTML解码与日期解析等容错处理。
Python爬取RSS源本质上是获取XML格式的HTTP响应内容,不需要复杂渲染,重点在于可靠地发送请求、处理编码和解析XML结构。
RSS本质是公开的XML文件,直接用requests发起GET请求即可。注意设置合理的User-Agent避免被拒绝,多数RSS不需登录或JS执行。
Python标准库xml.etree.ElementTree足够处理规范RSS(如RSS 2.0、Atom),无需额外安装。
RSS源质量参差不齐,需主动容错。
以下是一个最小可行脚本:
import requests
from xml.etree import ElementTree as ET
from urllib.parse import urljoin
url = "https://example.com/feed.xml"
headers = {"User-Agent": "Mozilla/5.0"}
try:
resp = re
quests.get(url, headers=headers, timeout=10)
resp.raise_for_status()
resp.encoding = "utf-8"
root = ET.fromstring(resp.text)
items = root.findall(".//item") or root.findall(".//{http://www.w3.org/2005/Atom}entry")
for item in items[:5]:
title = item.findtext("title") or item.findtext("{http://www.w3.org/2005/Atom}title")
link = item.findtext("link") or item.findtext("{http://www.w3.org/2005/Atom}link").get("href", "") if item.find("{http://www.w3.org/2005/Atom}link") else ""
print(title.strip(), urljoin(url, link))
except Exception as e:
print("获取失败:", e)
不复杂但容易忽略细节,关键是把RSS当普通XML文件对待,稳住请求、看清命名空间、做好异常兜底。