Files

37 lines
1.4 KiB
Python

import urllib.request
import xml.etree.ElementTree as ET
import sys
def get_feeds(category, source_url):
try:
req = urllib.request.Request(source_url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req, timeout=10) as response:
content = response.read()
root = ET.fromstring(content)
items = root.findall('.//item')
output = []
for item in items[:5]: # 每源取前5条
title = item.find('title').text
link = item.find('link').text
desc = item.find('description').text if item.find('description') is not None else ""
# 清洗摘要中的 HTML
summary = desc.split('<')[0][:150] + "..." if desc else "无摘要"
output.append(f"- [{title}]({link}) {summary}")
if output:
print(f"**{category}**")
print("\n".join(output))
print("\n")
except Exception as e:
print(f"Error fetching {source_url}: {e}", file=sys.stderr)
if __name__ == "__main__":
sources = {
"科技新闻 (CNBC)": "https://search.cnbc.com/rs/search/view.xml?partnerId=2000&keywords=technology",
"国际新闻 (BBC)": "https://feeds.bbci.co.uk/news/world/rss.xml",
"科技动态 (TechCrunch)": "https://techcrunch.com/feed/"
}
for cat, url in sources.items():
get_feeds(cat, url)