用起來記得把引數改改。
原始碼:
import itchat
import requests
import re
# 抓取網頁
def getHtmlText(url):
try:
r = requests。get(url,timeout=30)
r。raise_for_status()
r。encoding = r。apparent_encoding
return r。text
except:
return “”
# 自動回覆
# 封裝好的裝飾器,當接收到的訊息是Text,即文字訊息
@itchat。msg_register([‘Text’,‘Map’, ‘Card’, ‘Note’, ‘Sharing’, ‘Picture’])
def text_reply(msg):
# 當訊息不是由自己發出的時候
if not msg[‘FromUserName’] == Name[“pikachu”]:
# 回覆給好友
url = “http://www。tuling123。com/openapi/api?key=apikey&info=”
url = url + msg[‘Text’]
html = getHtmlText(url)
message = re。findall(r‘“text”:“。*?”’,html)
reply = eval(message[0]。split(‘:’)[1])
return reply
if __name__ == ‘__main__’:
itchat。auto_login()
# 獲取自己的UserName
friends = itchat。get_friends(update=True)[0:]
Name = {}
Nic = []
User = []
for i in range(len(friends)):
Nic。append(friends[i][“NickName”])
User。append(friends[i][“UserName”])
for i in range(len(friends)):
Name[Nic[i]] = User[i]
itchat。run()
原理:
這個功能還挺有用的,可以接入自己的資料庫,就可以查詢想要的資料啦。
作者的效果圖: