首页 > 解决方案 > 是否可以让机器人基于网站发送消息?

问题描述

我想做的是每当网站上有新东西时,我的不和谐机器人只会发送一条消息说“嘿,那里有新东西”。例如,有一个图书网站,他们上传了关于图书的新帖子及其描述,而我的机器人只是从该帖子中获取在线文本并将其发送到我的不和谐服务器。我希望它足够清楚。在这里,我有我用 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')

标签: pythondiscorddiscord.pybots

解决方案


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

问题的两个部分并不太难:

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

创建网络抓取工具

用于网页抓取的包完全取决于开发人员的愿望/需求。由于discord.pyuses asyncio,您应该使用异步解析器,例如aiohttp, orrequests-html与 or 相对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()协程创建一个会话来下载网页(替换"<url>"为网站的实际 URL,然后通过将页面 HTML与RECENT_HTML. RECENT_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装饰器包裹了一个协程,将其安排为以确定的时间间隔运行的后台任务。间隔(作为浮点数或整数)可以是秒、分钟、小时或三者的组合。

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

推荐阅读