我正在尝试编写一个显示当前新闻标题的程序。我尝试了python模块报纸,但灌输有问题。有谁知道完成这项任务的另一种方法?谢谢。
您可以使用提要解析器模块从您喜欢的新闻页获取 RSS 信息。
RSS 源示例:
- https://www.ft.com/news-feed
- http://www.reuters.com/tools/rss
- http://www.economist.com/rss
- https://www.rsssearchhub.com/feeds/
代码示例:
import feedparser
d = feedparser.parse('http://www.reddit.com/r/python/.rss')
print d['feed']['title']
print d['feed']['link']
print d.feed.subtitle
输出:
>>> Python
>>> http://www.reddit.com/r/Python/
>>> news about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
如果您有任何疑问,可以深入了解 PythonForBeginners Feedparser 教程。