直接上代码
-- 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中……\n')
uuid = itchat.get_QRuuid()
while uuid is None:
uuid = itchat.get_QRuuid()
time.sleep(1)
self.outputWritten('成功获取uuid\n')
if itchat.get_QR(uuid, picDir=r'%s' % os.path.join(current_path, 'qrcode.jpg')):
break
elif get_count >= 1:
self.outputWritten("获取二维码出错,请重启程序\n")
sys.exit()
return uuid
# 二维码登陆
def login_wechat(self):
try:
uuid = self.open_qr()
self.outputWritten("请扫描二维码\n")
waitForConfirm = False
while 1:
status = itchat.check_login(uuid)
if status == '200':
break
elif status == '201':
if waitForConfirm:
self.outputWritten('请进行确认\n')
waitForConfirm = True
elif status == '408':
self.outputWritten('重新加载二维码\n')
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\n' % userInfo['User']['NickName'])
itchat.start_receiving()
print('itchat.start_receiving(