Python和bs4一起阅读rss提要,bot发布到discord,我需要在它们前面加上“>”的引号

2024-09-27 18:00:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个机器人,它可以从一个论坛中抓取数据,并在一个不和谐的频道中发布帖子。我使用urllib读取RSS提要,并使用bs4解析它。假设我从RSS提要中获得:

<item>
        <title>What Music Do You Build Robots to?</title>
        <dc:creator><![CDATA[@TaranMayer TaranMayer ]]></dc:creator>
        <description><![CDATA[ <aside class="quote no-group" data-username="Kajunii42" data-post="37" data-topic="84065" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="20" height="20" src="https://www.vexforum.com/letter_avatar_proxy/v4/letter/k/e274bd/40.png" class="avatar"> Kajunii42:</div>
<blockquote>
<p>HECK YES DUDE F-777 IS KING!!! whats ur favorite song?</p>
</blockquote>
</aside>
<p>F-777 are the ones that are used in Geometry Dash all the time, right?</p> ]]></description>
        <link>https://www.vexforum.com/t/what-music-do-you-build-robots-to/84065/38</link>
        <pubDate>Wed, 02 Sep 2020 21:56:44 +0000</pubDate>
        <guid isPermaLink="false">www.vexforum.com-post-669155</guid>
</item>

在我们的论坛上,用户可以引用一篇或多篇以前的文章,这些文章显示在RSS提要的<blockquote>标记中。他们不必引用某人的话,所以它不会一直存在,我所问的也不会总是必要的

由于它将发布在discord上,我需要引号部分以>开始,以使其出现在引号块中。这是一个示例,说明了在bot发送不一致信息之前,我需要得到什么:

> Kajunii42:
>
> HECK YES DUDE F-777 IS KING!!! whats ur favorite song?

F-777 are the ones that are used in Geometry Dash all the time, right?

如何在<blockquote>标记中出现的每一行前面插入>字符


Tags: thetodivcomdatatitlewwwitem
1条回答
网友
1楼 · 发布于 2024-09-27 18:00:09

我想知道这是否符合你的要求

from simplified_scrapy import SimplifiedDoc, req
html = '''
<item>
        <title>What Music Do You Build Robots to?</title>
        <dc:creator><![CDATA[@TaranMayer TaranMayer ]]></dc:creator>
        <description><![CDATA[ <aside class="quote no-group" data-username="Kajunii42" data-post="37" data-topic="84065" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="20" height="20" src="https://www.vexforum.com/letter_avatar_proxy/v4/letter/k/e274bd/40.png" class="avatar"> Kajunii42:</div>
<blockquote>
<p>HECK YES DUDE F-777 IS KING!!! whats ur favorite song?</p>
</blockquote>
</aside>
<p>F-777 are the ones that are used in Geometry Dash all the time, right?</p> ]]></description>
        <link>https://www.vexforum.com/t/what-music-do-you-build-robots-to/84065/38</link>
        <pubDate>Wed, 02 Sep 2020 21:56:44 +0000</pubDate>
        <guid isPermaLink="false">www.vexforum.com-post-669155</guid>
</item>
'''
doc = SimplifiedDoc(html)
description = doc.getElement('item').getElement('description')
name = description.getElement('div', value='title').text
content = '> ' + name
blockquote = description.blockquote
if blockquote:
    content = content + '\n> ' + blockquote.text
content = content + '\n' + description.getElement('p', start='</aside>').text
print(content)

结果:

> Kajunii42:
> HECK YES DUDE F-777 IS KING!!! whats ur favorite song?
F-777 are the ones that are used in Geometry Dash all the time, right?

相关问题 更多 >

    热门问题