Python解析RSS库feedparser
1. 前言
Python 的 feedparser 库 1,可以直接解析 RSS 源。
2. 代码
安装 feedparser 库:
pip install feedparser
获得个人网站的前 14 项 RSS 的日期📅、标题和链接🔗 2, 3:
import feedparser
import datetime
from time import mktime
rss_url = 'http://xiaoledeng.github.io/index.xml'
def get_article():
title = []
link = []
date = []
rss_weekly = feedparser.parse(rss_url)
for i in range(0,14):
title.append(rss_weekly['entries'][i].title)
link.append(rss_weekly['entries'][i].link)
date.append((datetime.datetime.fromtimestamp(mktime(rss_weekly['entries'][i].published_parsed)) + datetime.timedelta(hours=8)).strftime("%Y-%m-%d"))
return title, link, date
title, link, date = get_article()
print('\n\nNew articles:\n\n')
for i in range(0,7):
print(f'{date[i]}',f'{title[i]}',f'{link[i]}\n')
print('\n\nRecent articles:\n\n')
for i in range(7,14):
print(f'{date[i]}',f'{title[i]}',f'{link[i]}\n')