【问题标题】:Is it possible to make a bot send messages based on a website?是否可以让机器人基于网站发送消息?
【发布时间】:2021-07-12 00:01:49
【问题描述】:

我想做的是,每当网站上有新内容时,我的不和谐机器人只会发送一条消息说“嘿,那里有新事物”。例如,有一个图书网站,他们上传了关于图书的新帖子及其描述,而我的机器人只是从该帖子中获取在线文本并将其发送到我的不和谐服务器。我希望它足够清楚。在这里,我有我用 Python 3.9 制作的基本不和谐机器人代码

import discord 
from discord.ext import commands

client = commands.Bot(command_prefix = '!')

@client.event 
async def on_ready():
    print("Bot is working.")

client.run('not today')

【问题讨论】:

  • 您可以使用this 每x 运行一次任务,以检查站点上的内容是否已更改。实际检查取决于您
  • 我认为最大的挑战是聆听网站的变化,我看到了两种选择:如果您有权修改网站以进行聆听,您可以将该网站与 IFTTT 等服务集成.另一方面,如果您无权修改网站,您可以使用BeautifulSoup 提供一些服务来解析网站 html/xml 代码并找到一些方法来识别新内容或您想要的任何内容,但这样真的难的。要构建信任侦听器/通知器,您必须控制这两个部分。
  • 我认为有关您是否拥有该网站的更多信息,因为如果您拥有它,那就更容易了。如果不是,那么前两个 cmets 是可能的方法(使用 discord.py 中的任务检查网站的内容是否每 x 秒更改一次

标签: python discord discord.py bots


【解决方案1】:

有关更多详细信息,我建议查看discord.ext.tasks module 的文档,它允许您为机器人运行后台任务。这对于更个性化的框架实现特别方便。

两部分题都不太难:

  1. 创建一个网络抓取工具,检查页面 HTML 中的更新
  2. 创建一个使用上述网络爬虫的后台任务。

创建网络抓取工具

用于网页抓取的包完全取决于开发人员的愿望/需求。由于discord.py 使用asyncio,因此您应该使用异步解析器,例如aiohttprequests-html,而不是urllibrequests,它们是阻塞的。

使用 AIOHTTP

import aiohttp

RECENT_HTML = ""

async def download_webpage():
    async with aiohttp.ClientSession() as session:
        async with session.get("<url>") as response:
            if response.status != 200:
                # Notify users that the website could not be scraped

            html = await response.text()
            if html != RECENT_HTML:
                # Notify users of changes within the website
                # An HTML parser could be used to identify specific changes within the HTML
                # Or you could just tell the members that a change occurred.
            RECENT_HTML = html

这些 download_webpage() 协程创建一个会话来下载网页(将 "&lt;url&gt;" 替换为网站的实际 URL,然后通过将页面 HTML 与 RECENT_HTML 进行比较来检查网页是否更改。RECENT_HTML只存储最新版本的被抓取的 HTML,用于比较。要检查的 HTML 不必存储为变量,例如可以写入文件。

如果 HTML 不同,您可以简单地通知成员,或者您可以使用 HTML 解析器来获取确切的差异。请注意,这些更改可能很微妙且无关紧要(例如,页面上的广告在检查之间发生了更改),因此我建议检查特定元素内的更改。 (但是,这样做超出了这个问题的范围。)

最后,将页面 HTML 的新副本存储在变量中(否则将存储最新版本的 HTML)。

带有请求-HTML

from requests_html import AsyncHTMLSession

RECENT_HTML = ""

async def download_webpage():
    asession = AsyncHTMLSession()
    response = await asession.get("<url>")
    if response.status_code != 200:
        # Notify users that the website could not be scraped
    
    html = response.html.text
    if html != RECENT_HTML:
        # Notify users of changes within the website
        # An HTML parser could be used to identify specific changes within the HTML
        # Or you could just tell the members that a change occurred.
    RECENT_HTML = html

创建后台任务

discord.ext.tasks.loop decorator 包裹了一个协程,将其调度为以确定的时间间隔运行的后台任务。间隔(作为浮点数或整数)可以是秒、分钟、小时或三者的组合。

from discord.ext import tasks

@tasks.loop(seconds=5.0)
async def my_task():
    # Do something that is repeated every 5 seconds

因此,将两者结合起来,您的网络爬虫任务可能如下所示:

import aiohttp
from discord.ext import tasks

RECENT_HTML = ""

@tasks.loop(hours=1)
async def download_webpage():
    async with aiohttp.ClientSession() as session:
        async with session.get("<url>") as response:
            if response.status != 200:
                # Notify users that the website could not be scraped

            html = await response.text()
            if html != RECENT_HTML:
                # Notify users of changes within the website
                # An HTML parser could be used to identify specific changes within the HTML
                # Or you could just tell the members that a change occurred.
            RECENT_HTML = html

【讨论】:

    【解决方案2】:

    您可以使用tasks.loop查看新闻:

    import bs4
    import aiohttp
    from discord.ext import tasks
    
    @tasks.loop(minutes=1)
    async def check_news():
      async with aiohttp.ClientSession() as ses:
        async with ses.get(your_url) as response:
          if response.status == 200:
            text = await response.text()
            soup = bs4.BeautifulSoup(text, "html.parser")
            #finding the news
            #if there is a new post, you can send it to spesific channel.
    

    如果你能分享链接,我可以提供进一步的帮助。

    【讨论】:

    • 嘿,我想我得到了我的机器人的正确链接,我只是 python 和不和谐机器人的初学者,所以我想知道你是否可以进一步帮助我。我们可以不和谐地聊天我的标签是 voltifer#2782
    • 我们当然可以不和谐地聊天,但如果我们在这里交谈,我们也会在这件事上帮助其他人。
    • 你能在 discord 上加我吗?
    猜你喜欢
    • 2020-05-18
    • 2022-01-03
    • 2019-09-20
    • 1970-01-01
    • 2018-06-03
    • 2019-11-25
    • 1970-01-01
    • 2021-08-03
    • 2017-11-20
    相关资源
    最近更新 更多