时间:2023-04-30 02:09:01 | 来源:网站运营
时间:2023-04-30 02:09:01 来源:网站运营
一首歌的时间,手把手搭建基于FC的网站:feedparser
它的feedparser.parse()
函数提供了解析RSS URL的功能,它可以将RSS连接解析为一个字典feed 对应一个字典
,每个entry包含一个文章的标题,作者,链接等内容。from flask import Flask, render_templateimport feedparserapp = Flask(__name__)@app.route('/')def index(): return render_template('index.html')@app.route('/rss', methods=["GET"])def rss(): feed = feedparser.parse(request.args.get('rssurl')) return render_template('rss.html', entries=feed.entries)if __name__ == "__main__": app.run(host="0.0.0.0", port=9000)
3.写入HTML页面<!doctype html><html><head><meta charset="utf-8"><title>RSS</title><!--引入外部CSS--><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"><!--引入外部JavaScript--><script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.min.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.slim.min.js"></script></head><body> <div class="container-fluid"> <div class="row-fluid"> <div class="span12"> <!--导航栏--> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <a class="navbar-brand" href="#">RSS</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">首页</a> </li> </ul> <form class="d-flex"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success" type="submit">搜索</button> </form> </div> </div> </nav> <!--内容列表--> <div class="accordion" id="accordionExample"> {% for entry in entries %} <div class="accordion-item"> <h2 class="accordion-header" id="heading{{ loop.index }}"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse{{ loop.index }}" aria-expanded="false" aria-controls="collapse{{ loop.index }}"> {{ entry.title }} | {{ entry.published }} </button> </h2> <div id="collapse{{ loop.index }}" class="accordion-collapse collapse" aria-labelledby="heading{{ loop.index }}" data-bs-parent="#accordionExample"> <div class="accordion-body"> {{ entry.summary | safe }} </div> </div> </div> {% endfor %} </div> </div> </div> </div></body></html>
文件创建完成后点击保存和部署,在应用详情页找到并访问FC给我们分配的域名。request.args.get('rssurl')
获取参数,因此在URL后面传入需要解析的RSS订阅地址参数就可以解析源并渲染成相应的阅读样式。feedparser
库相应的五行代码就完成了这个小实践。关键词:把手