python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码
前言
python的強大在于豐富的類庫,經常會看到幾行代碼就可以實現非常強大的功能。它可以做爬蟲、AI、自動化測試、小工具(搶票、抓包、微信消息抓取)等等。
本次我們來講講怎么來抓取微信消息?抓取微信微信消息以后我們可以做什么?
廢話不多說,直接進正題~
思路
一、引用的類庫
#日志
import logging
#正則
import re
#shutil模塊主要作用與拷貝文件用的
import shutil
#基于網頁版微信的python微信API
import itchat
#抓取消息的類型庫
from itchat.content import *
#pymysql來MySQL數據庫的連接
import pymysql
hotReload=True可以緩存登錄信息,防止重復登錄。登錄以后會在本地生成一個itchat.pkl文件.
def main():
#登錄 記錄登錄狀態 防止重復登錄
itchat.auto_login(hotReload=True)
itchat.run()
if __name__ == '__main__':
main()
三、接收消息
處理好友的文本類消息(文本、位置、名片、通知、分享),獲取消息并保存數據庫。用戶的撤回的消息轉發到文件助手,再也不怕看不到撤回的消息了.
#創建字典
storage = dict()
# 處理文本類消息
# 包括文本、位置、名片、通知、分享
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def simple_reply(msg):
try:
#聲明全局變量
global storage
output_info('simple_reply接收到的消息是:%s' % msg)
oldMsgId = ''
oldContent=''
#獲取消息id
msgId = msg['MsgId']
#獲取消息內容,去除首位空格
content = msg['Text'].strip()
#獲取消息類型
type = msg['Type']
#撤回消息轉發到文件傳輸助手
if msg['Type'] == 'Note':
#獲取撤回的消息id
oldMsgId = re.search('\(.*?)\', msg['Content']).group(1)
#字典中獲取撤回消息的內容
oldContent = storage[oldMsgId]['content']
tip = storage[oldMsgId]['name']+'撤回一條消息:'+oldContent
#轉發到微信文件助手
itchat.send(tip,'filehelper')
#非撤回消息
#找出誰發的消息
fromUserName = msg['FromUserName']
#如果不是騰訊推送的新聞,獲取發送消息的用戶微信名稱
if fromUserName != 'newsapp':
fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName']
toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName']
#保存到字典
storage[msgId] = {'name':fromUserName,'content':content}
#保存到數據庫
add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,'01','')
except Exception as e:
output_info('simple_reply處理消息異常:%s' % e)
處理好友的多媒體類消息(包括圖片、錄音、文件、視頻),獲取的文件下載到本地,并在數據庫中記錄文件信息。
# 處理多媒體類消息
# 包括圖片、錄音、文件、視頻
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
try:
global storage
output_info('download_files接收到的消息是:%s' % msg)
msgId = msg['MsgId']
type = msg['Type']
#找出誰發的消息
fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName']
toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName']
#文件保存的路徑
fileName = "weChatFile/"+msg['FileName']
#把消息放入字典
storage[msgId] = {'name':fromUserName,'content':fileName}
#保存消息
add_msg(msgId,"",fromUserName,toUserName,fileName,'',type,'01','')
#msg['Text']是一個文件下載函數
#先將文件下載下來
msg['Text'](msg['FileName'])
output_info('download_files正在保存文件:%s' % fileName)
#文件下載到本地
shutil.move(msg['FileName'], fileName)
print("文件:"+fileName+"保存成功!")
output_info('download_files文件:%s:'+fileName+'保存成功!')
except Exception as e:
output_info('download_files處理文件異常:%s' % e)
處理群里面的文本類消息(文本、位置、名片、通知、分享)
# 處理文本類消息
# 包括文本、位置、名片、通知、分享
#isGroupChat = True接受群消息
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING], isGroupChat = True)
def groupchat_reply(msg):
try:
#打印接收到的消息
output_info('groupchat_reply接收到的~~消息是:%s' % msg)
#獲取消息id
msgId = msg['MsgId']
#獲取消息類型
type = msg['Type']
oldContent = ""
oldMsgId = ""
groupName = ""
toUserName = ""
fromUserName = ""
#獲取消息來源
userName=msg['FromUserName']
#@@是群轉過來的消息
if userName.find('@@') !=-1:
#獲取群名稱
groupName = itchat.search_chatrooms(userName=msg['FromUserName'])['NickName']
#獲取說話人的微信名
fromUserName = msg['ActualNickName']
#獲取接收微信的用戶信息
toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName']
else:
groupName = itchat.search_chatrooms(userName=msg['ToUserName'])['NickName']
fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName']
toUserName = groupName
if msg['Type'] == 'Note':
oldMsgId = re.search('\(.*?)\', msg['Content']).group(1)
oldContent = storage[oldMsgId]['content']
tip = storage[oldMsgId]['name']+'撤回一條群消息:'+oldContent
itchat.send(tip,'filehelper')
content = msg['Text'].strip()
storage[msgId] = {'name':fromUserName,'content':content}
add_msg(msgId,oldMsgId,fromUserName,toUserName,str(content),oldContent,type,'02',str(groupName))
# if msg['isAt']:
# itchat.send(u'@%s\\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
except Exception as e:
output_info('groupchat_reply處理群里的消息異常:%s' % e)
處理群里面的多媒體類消息(包括圖片、錄音、文件、視頻)
# 處理多媒體類消息
# 包括圖片、錄音、文件、視頻
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat = True)
def groupchat_files(msg):
try:
output_info('groupchat_files接收到的~~群~~消息是:%s' % msg)
msgId = msg['MsgId']
type = msg['Type']
groupName = ""
toUserName = ""
fromUserName = ""
userName=msg['FromUserName']
#@@是群轉過來的消息
if userName.find('@@') !=-1:
groupName = itchat.search_chatrooms(userName=msg['FromUserName'])['NickName']
fromUserName = msg['ActualNickName']
toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName']
else:
groupName = itchat.search_chatrooms(userName=msg['ToUserName'])['NickName']
fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName']
toUserName = groupName
fileName = "weChatGroupFile/"+msg['FileName']
storage[msgId] = {'name':fromUserName,'content':fileName}
output_info("groupchat_files正在保存文件:%s"+fileName)
add_msg(msgId,"",fromUserName,toUserName,fileName,'',type,'01','')
msg['Text'](msg['FileName'])
#移動
shutil.move(msg['FileName'], fileName)
output_info('groupchat_files文件:%s:'+fileName+'保存成功!')
# if msg['isAt']:
# itchat.send(u'@%s\\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
except Exception as e:
output_info('groupchat_files處理群里的消息異常:%s' % e)
四、保存數據
直接上源碼,都可以看懂。
#添加數據庫
def add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,source,groupName):
try:
#鏈接數據庫
conn = pymysql.connect(host='localhost', user = "root", passwd="root", db="wechat", port=3306, charset="utf8mb4")
# 使用 cursor() 方法創建一個游標對象 cursor
#用來獲得python執行Mysql命令的方法
cursor = conn.cursor()
sql = "INSERT INTO `ge_wechat_msg` (`id`, `msgId`, `oldMsgId`, `fromUserName`, `toUserName`,`content`, `oldContent`, `type`, `source`,`groupName`, `makedate`, `modifydate`) VALUES (NULL, '"+msgId+"', '"+oldMsgId+"', '"+fromUserName+"','"+toUserName+"','"+content+"', '"+oldContent+"', '"+type+"','"+source+"','"+groupName+"', now(), NOW())"
count = cursor.execute(sql)
#判斷是否成功
if count > 0:
output_info('添加數據成功')
#提交事務
conn.commit()
except Exception as e:
conn.rollback()
output_info('添加數據庫異常:%s' % e)
五、打印日志
logging.basicConfig(filename='../LOG/weChat02.log',format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level = logging.INFO,filemode='a',datefmt='%Y-%m-%d%I:%M:%S %p')
def output_info(msg):
try:
print('[INFO] %s' % msg)
logging.info('u'+msg)
except Exception as e:
print('記錄日志異常:'+e)
總結:
上面就是全部代碼了,那么接收到微信消息,我們可以做什么呢?
對接圖靈機器人,再也不怕打游戲,沒時間和女朋友聊天了(祈禱別被發現)。
可以給朋友或者給群聊一鍵群發消息。
對于微商來說,會有非常多的群,所以python可以幫你管理群聊,分析用戶行為。
等等~~~
總結
以上是生活随笔為你收集整理的python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动态分辨率是什么意思_什么是1080p、
- 下一篇: 虚拟币交易平台开发_虚拟币软件开发未来发