Python程序设计之聊天机器人(爬虫)

420 阅读2分钟
参与拿奖:本文已参与「新人创作礼」活动,一起开启掘金创作之路
ps:代码文末自取
1.运行结果

主要实现从网上获取聊天机器人的反馈,实现聊天功能 image.png

2.UI设计

2.1 输入框与聊天框

root=tkinter.Tk() # 创建窗口
root.title(title) # 窗口名字
root['height']=500
root['width']=500
var_Message=tkinter.StringVar() # 信息
var_Message.set('')

label_m=tkinter.Label(root,text="Message:",width=60)
label_m.place(x=10,y=60,width=60,height=20)
entry_m=tkinter.Entry(root,textvariable=var_Message)
entry_m.place(x=80,y=60,width=300,height=20)

listBox=tkinter.Listbox(root)
listBox.place(x=80,y=150,width=300,height=300)

2.2 按钮


button_d=tkinter.Button(root,text="Deliver",width=36,command=Deliver) # 发送按钮
button_d.place(x=100,y=100,width=60,height=20)

button_cl=tkinter.Button(root,text="Clean",width=36,command=Clean) # 清空按钮
button_cl.place(x=200,y=100,width=60,height=20)

button_q=tkinter.Button(root,text="Quit",width=36,command=Quit) # 退出按钮
button_q.place(x=400,y=430,width=60,height=20)
3.聊天功能
x=entry_m.get() 获取发送的内容
listBox.insert(r, "你: "+x) # 插入聊天框 
r+=1
x = urllib.parse.quote(x) # 发送请求
link = urllib.request.urlopen(
    "http://nlp.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=%7B%22sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcf1a462e6c58e6%22%2C%22body%22%3A%7B%22content%22%3A%22" + x + "%22%7D%2C%22type%22%3A%22txt%22%7D") # 请求链接
html_doc = link.read().decode() # 解码
reply_list = re.findall(r'"content":"(.+?)\r\n"', html_doc) # 使用正则找到返回的内容
temp=reply_list[-1].replace('\r\n',',') # 字符串替换
listBox.insert(r,"小i: "+temp) # 插入到聊天框
r+=1
Clean() # 清空输入框信息
4.完整代码
import urllib.request
import re
import tkinter

r=0

def window(title):
    root=tkinter.Tk() # 创建窗口
    root.title(title) # 窗口名字
    root['height']=500
    root['width']=500
    var_Message=tkinter.StringVar() # 信息
    var_Message.set('')

    label_m=tkinter.Label(root,text="Message:",width=60)
    label_m.place(x=10,y=60,width=60,height=20)
    entry_m=tkinter.Entry(root,textvariable=var_Message)
    entry_m.place(x=80,y=60,width=300,height=20)

    listBox=tkinter.Listbox(root)
    listBox.place(x=80,y=150,width=300,height=300)

    def Clean():
        try:
            var_Message.set('')
        except:
            print("Error!")

    def Deliver():
        global r
        x=entry_m.get()
        listBox.insert(r, "你: "+x)
        r+=1
        x = urllib.parse.quote(x)
        link = urllib.request.urlopen(
            "http://nlp.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=%7B%22sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcf1a462e6c58e6%22%2C%22body%22%3A%7B%22content%22%3A%22" + x + "%22%7D%2C%22type%22%3A%22txt%22%7D")
        html_doc = link.read().decode()
        reply_list = re.findall(r'"content":"(.+?)\r\n"', html_doc)
        temp=reply_list[-1].replace('\r\n',',')
        listBox.insert(r,"小i: "+temp)
        r+=1
        Clean()

    def Quit():
        root.destroy()


    button_d=tkinter.Button(root,text="Deliver",width=36,command=Deliver) # 发送按钮
    button_d.place(x=100,y=100,width=60,height=20)

    button_cl=tkinter.Button(root,text="Clean",width=36,command=Clean) # 清空按钮
    button_cl.place(x=200,y=100,width=60,height=20)

    button_q=tkinter.Button(root,text="Quit",width=36,command=Quit) # 退出按钮
    button_q.place(x=400,y=430,width=60,height=20)

    root.mainloop()


if __name__=="__main__":
    window('Chatting')