日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码

發(fā)布時(shí)間:2025/3/21 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

python的強(qiáng)大在于豐富的類庫,經(jīng)常會(huì)看到幾行代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能。它可以做爬蟲、AI、自動(dòng)化測(cè)試、小工具(搶票、抓包、微信消息抓取)等等。

本次我們來講講怎么來抓取微信消息?抓取微信微信消息以后我們可以做什么?

廢話不多說,直接進(jìn)正題~

思路

一、引用的類庫

#日志

import logging

#正則

import re

#shutil模塊主要作用與拷貝文件用的

import shutil

#基于網(wǎng)頁版微信的python微信API

import itchat

#抓取消息的類型庫

from itchat.content import *

#pymysql來MySQL數(shù)據(jù)庫的連接

import pymysql

hotReload=True可以緩存登錄信息,防止重復(fù)登錄。登錄以后會(huì)在本地生成一個(gè)itchat.pkl文件.

def main():

#登錄 記錄登錄狀態(tài) 防止重復(fù)登錄

itchat.auto_login(hotReload=True)

itchat.run()

if __name__ == '__main__':

main()

三、接收消息

處理好友的文本類消息(文本、位置、名片、通知、分享),獲取消息并保存數(shù)據(jù)庫。用戶的撤回的消息轉(zhuǎn)發(fā)到文件助手,再也不怕看不到撤回的消息了.

#創(chuàng)建字典

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']

#獲取消息內(nèi)容,去除首位空格

content = msg['Text'].strip()

#獲取消息類型

type = msg['Type']

#撤回消息轉(zhuǎn)發(fā)到文件傳輸助手

if msg['Type'] == 'Note':

#獲取撤回的消息id

oldMsgId = re.search('\(.*?)\', msg['Content']).group(1)

#字典中獲取撤回消息的內(nèi)容

oldContent = storage[oldMsgId]['content']

tip = storage[oldMsgId]['name']+'撤回一條消息:'+oldContent

#轉(zhuǎn)發(fā)到微信文件助手

itchat.send(tip,'filehelper')

#非撤回消息

#找出誰發(fā)的消息

fromUserName = msg['FromUserName']

#如果不是騰訊推送的新聞,獲取發(fā)送消息的用戶微信名稱

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}

#保存到數(shù)據(jù)庫

add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,'01','')

except Exception as e:

output_info('simple_reply處理消息異常:%s' % e)

處理好友的多媒體類消息(包括圖片、錄音、文件、視頻),獲取的文件下載到本地,并在數(shù)據(jù)庫中記錄文件信息。

# 處理多媒體類消息

# 包括圖片、錄音、文件、視頻

@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']

#找出誰發(fā)的消息

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']是一個(gè)文件下載函數(shù)

#先將文件下載下來

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']

#@@是群轉(zhuǎn)過來的消息

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']

#@@是群轉(zhuǎn)過來的消息

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'])

#移動(dòng)

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)

四、保存數(shù)據(jù)

直接上源碼,都可以看懂。

#添加數(shù)據(jù)庫

def add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,source,groupName):

try:

#鏈接數(shù)據(jù)庫

conn = pymysql.connect(host='localhost', user = "root", passwd="root", db="wechat", port=3306, charset="utf8mb4")

# 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor

#用來獲得python執(zhí)行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('添加數(shù)據(jù)成功')

#提交事務(wù)

conn.commit()

except Exception as e:

conn.rollback()

output_info('添加數(shù)據(jù)庫異常:%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)

總結(jié):

上面就是全部代碼了,那么接收到微信消息,我們可以做什么呢?

對(duì)接圖靈機(jī)器人,再也不怕打游戲,沒時(shí)間和女朋友聊天了(祈禱別被發(fā)現(xiàn))。

可以給朋友或者給群聊一鍵群發(fā)消息。

對(duì)于微商來說,會(huì)有非常多的群,所以python可以幫你管理群聊,分析用戶行為。

等等~~~

總結(jié)

以上是生活随笔為你收集整理的python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。