美文网首页码农的世界Python新世界python热爱者
是时候展现真正的Python技术了,Python登录微信获取账号

是时候展现真正的Python技术了,Python登录微信获取账号

作者: Python新世界 | 来源:发表于2018-09-05 13:39 被阅读11次

    软件

    image

    软件功能

    利用python登陆微信并获取好友的群聊、公众号、列表

    使用方法:

    点击登陆后就可以登陆到你的微信账号上获取信息.

    更多功能期待进一步开发。

    其他说明:

    学习Python中有不明白推荐加入交流群号:

    前面548中间377后面875

    群里有志同道合的小伙伴,互帮互助,

    群里有不错的学习教程!

    原本是想做一个定时给好友发送微信消息的工具,结果发现好友获取不全,就暂时告一段落。代码在文末开放出来供大家一起研究!

    image

    微信数据库表信息

    这里需要介绍的是微信数据库中哪些数据表是我们这一次关心需要展示出来的,这个就要看我们需要展示什么数据了?这里我们把能展示的都展示出来吧:

    第一

    展示聊天记录表:message主要包括聊天对象信息和聊天内容信息,这个主要在message表中:场景:老婆发现老公的聊天记录中有什么危害夫妻关系的内容。字段:createTime:聊天时间;talker:聊天对象;content:聊天内容。

    image

    第二

    群聊信息表:chatroom这个表中主要包含了群聊信息,包括群聊的成员信息和群主等信息场景:老婆查看老公都有些什么群,群里有什么女的不能看的内容。字段:chatroomname:群聊id;memberlist:群成员id;displayname:群成员昵称;roomowner:群主id

    image

    第三

    个人信息表:userinfo这个表格中主要包含自己信息的,包括昵称,地区,微信号等场景:这里的信息老婆都知道的这里用id作为区分各个字段值,id=2表示微信id;id=4表示微信昵称;id=6表示电话等。

    image

    第四

    好友头像信息表:img_flag这个表中主要包括当前微信好友的头像信息场景:老婆发现老公的通讯录中有一个昵称好友很诡异,通过查看头像发现是个影响婚姻的人。字段:username:表示微信id;reversed1:表示好友头像

    image

    第五

    通讯录信息表:rcontact这个表中主要包括通讯录中好友信息场景:老婆看看老公的好友中有几个微信婚姻关系的人。字段:username:表示微信id;alias:表示微信名;nickname:表示微信昵称

    第六

    设备登录表:SafeDeviceInfo主要包括账号在哪些设备中登录过,包含设备名和系统以及登录时间等场景:老婆想看看老公的微信号在哪些设备登录过,搞不好有一个设备的主人就是威胁婚姻关系的人。字段:uid:表示设备id;name:表示设备名;devicetype:表示设备系统;createtime:表示登录时间

    image

    我们主要就是利用和保存这些最关键的几张表格信息下面就用一张图来表示上面的各个表格之间的关系:

    image

    所以有了这些表格我们就可以开发出功能全面的UI界面的数据库信息查询和备份工具了,具体代码不多说了,因为没啥技术可说的,C++中进行查询操作然后把数据传回到java中进行UI展示即可,大致界面如下:

    image

    我们只需要打开微信专属的数据库文件夹MicroMsg即可操作了,方便快捷

    image

    完整的Python源代码:

    -- coding: utf-8 --

    """

    Module implementing MainWindow.

    """

    from PyQt5.QtCore import QThread, pyqtSignal, Qt, pyqtSlot

    from PyQt5.QtWidgets import *

    from PyQt5 import QtGui

    from Ui_cronSendMessage import Ui_MainWindow

    import sys

    import itchat

    from itchat.content import *

    import datetime

    import time

    import os

    import requests

    import json

    import re

    from urllib.request import urlretrieve

    import traceback

    current_path = os.path.dirname(os.path.abspath(file))

    登陆微信

    class LoginWechat(QThread):

    自定义一个信号

    finished_signal = pyqtSignal(object)

    def init(self, parent=None, label=None, scroll_widget_layout=None, refresh_button=None, exit_button=None):

    super().init(parent)

    self.l = label

    self.scroll_widget_layout = scroll_widget_layout

    self.refresh_button = refresh_button

    self.exit_button = exit_button

    在控制台中写入信息

    def outputWritten(self, text=None):

    cursor = self.l.textCursor()

    cursor.movePosition(QtGui.QTextCursor.End)

    cursor.insertText(text)

    self.l.setTextCursor(cursor)

    self.l.ensureCursorVisible()

    获取uuid

    def open_qr(self):

    for get_count in range(1):

    self.outputWritten('获取uuid中…… ')

    uuid = itchat.get_QRuuid()

    while uuid is None:

    uuid = itchat.get_QRuuid()

    time.sleep(1)

    self.outputWritten('成功获取uuid ')

    if itchat.get_QR(uuid, picDir=r'%s' % os.path.join(current_path, 'qrcode.jpg')):

    break

    elif get_count >= 1:

    self.outputWritten("获取二维码出错,请重启程序 ")

    sys.exit()

    return uuid

    二维码登陆

    def login_wechat(self):

    try:

    uuid = self.open_qr()

    self.outputWritten("请扫描二维码 ")

    waitForConfirm = False

    while 1:

    status = itchat.check_login(uuid)

    if status == '200':

    break

    elif status == '201':

    if waitForConfirm:

    self.outputWritten('请进行确认 ')

    waitForConfirm = True

    elif status == '408':

    self.outputWritten('重新加载二维码 ')

    time.sleep(3)

    uuid = self.open_qr()

    waitForConfirm = False

    userInfo = itchat.web_init()

    print(userInfo)

    itchat.show_mobile_login()

    print('itchat.show_mobile_login() 执行完成!')

    itchat.get_friends(True)

    itchat.get_friends()

    print('itchat.get_friends(update=True)[0:] 执行完成!')

    self.outputWritten('登陆成功!账号为:%s ' % userInfo['User']['NickName'])

    itchat.start_receiving()

    print('itchat.start_receiving() 执行完成!')

    self.refresh_button.setText("已登录:{}".format(userInfo['User']['NickName']))

    self.exit_button.setEnabled(True)

    except Exception as e:

    print("登录出错:", e)

    self.outputWritten('登陆出错:{} '.format(e))

    try:

    获取群聊列表

    chatrooms = itchat.get_chatrooms()

    print('chatrooms = itchat.get_chatrooms() 执行完成!')

    print(type(chatrooms))

    return chatrooms

    except Exception as e:

    self.outputWritten("获取群聊列表出错:{} ".format(e))

    try:

    获取好友列表

    friends = itchat.get_friends()

    print('friends = itchat.get_friends() 执行完成!')

    print(type(friends))

    return friends

    except Exception as e:

    self.outputWritten("获取群聊列表出错:{} ".format(e))

    try:

    获取好友列表

    mps = itchat.get_mps()

    print('mps = itchat.get_mps() 执行完成!')

    print(type(mps))

    return mps

    except Exception as e:

    self.outputWritten("获取群聊列表出错:{} ".format(e))

    if chatrooms and friends and mps:

    return [chatrooms, friends, mps]

    def run(self):

    try:

    self.refresh_button.setEnabled(False)

    self.exit_button.setEnabled(True)

    self.finished_signal.emit(self.login_wechat())

    except Exception as e:

    self.outputWritten("运行登录线程出错:{} ".format(e))

    class MainWindow(QMainWindow, Ui_MainWindow):

    """

    Class documentation goes here.

    """

    def init(self, parent=None):

    """

    Constructor

    @param parent reference to the parent widget

    @type QWidget

    """

    super(MainWindow, self).init(parent)

    self.setupUi(self)

    在控制台中写入信息

    def outputWritten(self, text=None):

    获取文本框中文本的游标

    cursor = self.textEdit.textCursor()

    将游标位置移动到当前文本的结束处

    cursor.movePosition(QtGui.QTextCursor.End)

    写入文本

    cursor.insertText(text)

    设置文本的游标为创建了cursor

    self.textEdit.setTextCursor(cursor)

    self.textEdit.ensureCursorVisible()

    '''

    ItChat登陆功能

    '''

    @staticmethod

    def _show_message(message):

    print('{}'.format(message))

    获取群聊复选框选择状态

    def checkChatRoom(self, state):

    try:

    checkBox = self.sender()

    if state == Qt.Unchecked:

    self.outputWritten(u'取消选择了: '.format(checkBox.id_, checkBox.text()))

    self.chatroom_list.remove(self.chatroom_dict[checkBox.text()])

    elif state == Qt.Checked:

    self.outputWritten(u'选择了: '.format(checkBox.id_, checkBox.text()))

    self.chatroom_list.append(self.chatroom_dict[checkBox.text()])

    except Exception as e:

    self.outputWritten("获取群聊选择状态失败:{} ".format(e))

    生成群聊列表

    def generate_chatroom(self, chatrooms):

    清空原有群里列表

    while self.gridLayout.count():

    item = self.gridLayout.takeAt(0)

    widget = item.widget()

    widget.deleteLater()

    chatrooms = chatrooms[0]

    self.chatroom_dict = dict()

    try:

    for c, i in zip(chatrooms, range(len(chatrooms))):

    print(c['NickName'], c['UserName'])

    checkbox = QCheckBox(c['NickName'])

    checkbox.id_ = i

    self.chatroom_dict[c['NickName']] = c['UserName']

    checkbox.stateChanged.connect(self.checkChatRoom) # 1

    self.gridLayout.addWidget(checkbox)

    self.horizontalLayout_3.addWidget(self.checkBox_2)

    self.outputWritten("生成群聊成功! ")

    except Exception as e:

    print(e)

    生成好友列表

    def generate_friends(self, chatrooms):

    清空原有群里列表

    while self.verticalLayout_4.count():

    item = self.verticalLayout_4.takeAt(0)

    widget = item.widget()

    widget.deleteLater()

    chatrooms = chatrooms[1]

    self.chatroom_dict = dict()

    try:

    for c, i in zip(chatrooms, range(len(chatrooms))):

    print(c['NickName'], c['UserName'])

    checkbox = QCheckBox(c['NickName'])

    checkbox.id_ = i

    self.chatroom_dict[c['NickName']] = c['UserName']

    checkbox.stateChanged.connect(self.checkChatRoom) # 1

    self.verticalLayout_4.addWidget(checkbox)

    self.horizontalLayout_3.addWidget(self.checkBox_2)

    self.outputWritten("生成好友成功! ")

    except Exception as e:

    print(e)

    生成公众号列表

    def generate_mps(self, chatrooms):

    清空原有群里列表

    while self.verticalLayout_6.count():

    item = self.verticalLayout_6.takeAt(0)

    widget = item.widget()

    widget.deleteLater()

    chatrooms = chatrooms[2]

    self.chatroom_dict = dict()

    try:

    for c, i in zip(chatrooms, range(len(chatrooms))):

    print(c['NickName'], c['UserName'])

    checkbox = QCheckBox(c['NickName'])

    checkbox.id_ = i

    self.chatroom_dict[c['NickName']] = c['UserName']

    checkbox.stateChanged.connect(self.checkChatRoom) # 1

    self.verticalLayout_6.addWidget(checkbox)

    self.horizontalLayout_3.addWidget(self.checkBox_2)

    self.outputWritten("生成公众号成功! ")

    except Exception as e:

    print(e)

    @pyqtSlot()

    def on_pushButton_clicked(self):

    """

    登录微信 - 线程

    """

    登录微信 - 线程

    try:

    self.login_wechat_thread = LoginWechat(

    label=self.textEdit,

    scroll_widget_layout=self.verticalLayout,

    refresh_button=self.pushButton,

    exit_button=self.pushButton_2,

    )

    self.login_wechat_thread.finished_signal.connect(self.generate_chatroom)

    self.login_wechat_thread.finished_signal.connect(self.generate_friends)

    self.login_wechat_thread.finished_signal.connect(self.generate_mps)

    self.login_wechat_thread.finished_signal.connect(self.generate_chatroom)

    self.login_wechat_thread.start()

    except Exception as e:

    print("执行登录线程出错:", e)

    self.outputWritten("执行登录线程出错:{} ".format(e))

    @pyqtSlot()

    def on_pushButton_2_clicked(self):

    """

    注销按钮

    """

    设置登录按钮为激活状态

    self.pushButton.setEnabled(True)

    在文本控制台中输入

    self.outputWritten("退出微信登录 ")

    注销微信登录

    itchat.logout()

    设置注销按钮为禁用状态

    self.pushButton_2.setEnabled(False)

    更改登陆按钮

    self.pushButton.setText("登录")

    if name == 'main':

    app = QApplication(sys.argv)

    ui = MainWindow()

    ui.show()

    sys.exit(app.exec_())

    相关文章

      网友评论

        本文标题:是时候展现真正的Python技术了,Python登录微信获取账号

        本文链接:https://www.haomeiwen.com/subject/nxdawftx.html