需要用机器人来美化

2024-06-02 05:25:15 发布

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

我想要一个网址来触发它并从BeautifulSoup得到响应,我对python相当陌生,这只是我的第三个项目。在

#!/usr/bin/env python

import botlib
import urllib
import BeautifulSoup

class StaffBot(botlib.Bot):
    def __init__(self, server, channel, nick, password=None):
        botlib.Bot.__init__(self, server, 6667, channel, nick)

        if password != None:
            self.protocol.privmsg("nickserv", "identify" % password)
    def __actions__(self):
        botlib.Bot.__actions__(self)

        if botlib.check_found(self.data, ".downloads"):
            username = self.get_username()

            self.protocol.privmsg(self.channel, "%s: response" % username)          

if __name__ == "__main__":
    StaffBot("irc.rizon.net", "#chan", "nick").run()        

万一我做错了。。。。 http://pastebin.com/AhrssPVW

我正在使用的beauthulsoup脚本。在

^{pr2}$

编辑

我有点失败了,我想用say代替下载网站.net/viewtopic.php。像文(文)这样回复网页下面的标题。在

somone>>>website.net/viewtopic.php?f=6&t=10960                                                                                           
bot>>>WebsiteName • Viewtopic - topicname

Tags: importselfnetifserverinitdefbot
1条回答
网友
1楼 · 发布于 2024-06-02 05:25:15

也许你为你的用户创建了一个命令

if botlib.check_found(self.data, "!download"):
  # must be !download <url>
  url = self.data.split()[1] # < i don't know if the library adds anything else to self.data
  soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url))
  print soup.title.string

这要求用户在!download命令后包含一个url。在

所以房间里的用户可以输入!download website.net/viewtopic.php你的机器人会检查它是否是一个下载命令check_found(self.data, "!download")。然后它需要获取下载url。split()将一个字符串拆分为空白的列表。要获取的url将是列表[1]中的第二项。然后可以使用beautifulsoup获取/解析该url。在

相关问题 更多 >