【发布时间】:2015-08-30 16:08:02
【问题描述】:
我正在尝试创建一个程序,从 /r/Jokes 打印出前 5 个笑话,但我在格式化它以使其看起来不错时遇到了一些问题。我想把它设置成这样。
Post Title: Post Content
例如,这里是直接来自 RSS 提要的笑话之一:
<item>
<title>What do you call a stack of pancakes?</title>
<link>https://www.reddit.com/r/Jokes/comments/3ix348/what_do_you_call_a_stack_of_pancakes/</link>
<guid isPermaLink="true">https://www.reddit.com/r/Jokes/comments/3ix348/what_do_you_call_a_stack_of_pancakes/</guid>
<pubDate>Sun, 30 Aug 2015 03:18:00 +0000</pubDate>
<description><!-- SC_OFF --><div class="md"><p>A balanced breakfast</p> </div><!-- SC_ON --> submitted by <a href="http://www.reddit.com/user/TheRealCreamytoast"> TheRealCreamytoast </a> <br/> <a href="http://www.reddit.com/r/Jokes/comments/3ix348/what_do_you_call_a_stack_of_pancakes/">[link]</a> <a href="https://www.reddit.com/r/Jokes/comments/3ix348/what_do_you_call_a_stack_of_pancakes/">[2 comments]</a></description>
</item>
我目前正在打印标题,后跟一个冒号和一个空格,然后是描述。但是,它会打印所有文本,包括链接、作者和所有 HTML 标记。我如何才能获得段落标签内的文本。
谢谢,
编辑:这是我的代码:
d = feedparser.parse('https://www.reddit.com/r/cleanjokes/.rss')
print("")
print("Pulling latest jokes from Reddit. https://www.reddit.com/r/cleanjokes")
print("")
time.sleep(0.8)
print("Displaying First 5 Jokes:")
print("")
print(d['entries'][0]['title'] + ": " + d['entries'][0]['description'])
print(d['entries'][1]['title'] + ": " + d['entries'][1]['description'])
print(d['entries'][2]['title'] + ": " + d['entries'][2]['description'])
print(d['entries'][3]['title'] + ": " + d['entries'][3]['description'])
print(d['entries'][4]['title'] + ": " + d['entries'][4]['description'])
这只是获取前 5 个条目。我需要做的是将冒号后的描述字符串格式化为仅包含段落标签内的文本。
【问题讨论】:
-
标题是怎么得到的? (我想看一些代码,它会帮助我(也许))。
-
用代码更新了 OP。
标签: python xml rss reddit feedparser