php用正则表达抓取网页中文章(如何利用网页聊天机器人,做一个自动聊天的微信机器人)
优采云 发布时间: 2022-02-19 21:10php用正则表达抓取网页中文章(如何利用网页聊天机器人,做一个自动聊天的微信机器人)
我无事可做(其实还在期末考试中),当我在思考如何使用手中的服务器做某事时,我找到了python的itchat库,以为我可以再次使用网络聊天机器人做一个自动聊天微信机器人。
大致思路很简单,使用itchat登录微信,接收消息,通过聊天机器人接口发送给聊天机器人,然后通过itchat将接收到的消息发送给用户,实现自动聊天。
经过多次搜索,我发现了一个在线聊天机器人,它的聊天相当智能(带有有趣的语气),
在浏览器里找界面还是有点乱,用burp抓包更清楚:
我们的每一条消息都要通过GET方法传入,然后返回数据中的内容应该收录回复内容,尝试用python解码:
下一步是构造 url。我的想法是把url解码,然后拼接,然后发送url编码。
使用正则模式从返回的数据中匹配我们想要的数据:
由于可以得到返回的数据,所以可以使用itchat库从微信接收和发送:
运行后会生成二维码图片,扫码登录微信网页版。
关于 itchat 库:
操作很简单。
所有代码:
import itchat,code,unicodedata
import requests,re
from urllib.parse import quote,unquote
def get_reply(data):
ini = "{'sessionId':'09e2aca4d0a541f88eecc77c03a8b393','robotId':'webbot','userId':'462d49d3742745bb98f7538*敏*感*词*2f9f874','body':{'content':'" + data + "'},'type':'txt'}&ts=1529917589648"
url = "http://i.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=" + quote(ini)
cookie = {"cnonce": "808116", "sig": "0c3021aa5552fe597bb55448b40ad2a90d2dead5",
"XISESSIONID": "hlbnd1oiwar01dfje825gavcn", "nonce": "273765", "hibext_instdsigdip2": "1"}
r = requests.get(url, cookies=cookie)
print(r.text)
pattern = re.compile(r'\"fontColor\":0,\"content\":\"(.*?)\"')
result = pattern.findall(r.text)
print(result[1])
return result[1]
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
print(msg['Text'])
print(msg['FromUserName'])
datas=get_reply(msg['Text'])[:-4]
print(datas)
itchat.send(datas, toUserName=msg['FromUserName'])
itchat.auto_login()
itchat.run()
小白播放器,正常使用不是很好,代码不规范,不要抱怨。
另外,如果你使用图灵机器人或者小黄鸡提供的API,效果会更好,但是网上教程已经很多了,这里换个思路。
文章同步到我的博客:
转载并注明出处