Python imitates the message sending function of the web version, and python sends messages
This version of the web version is cumbersome, but not difficult, without encryption throughout the process. If you are interested, you can try to have fun. If you are interested, you can perfect it and make some interesting things.
Development Environment: Windows 10
Development language: Python3.6
Development Tool: pycharm
Packet capture tool: fiddler
The captured package is as follows:
Import requestsimport timeimport refrom bs4 import BeautifulSoupimport jsonimport randomfrom copyheaders import handler = {B 'host': B 'wx .qq.com', B 'Connection': B 'keep-alive ', B 'Cache-control': B' max-age = 0', B 'User-agent': B' Mozilla/5.0 (Windows NT 10.0; WOW64) appleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36 ', B' Upgrade-Insecure-Requests ': B' 1', B' Accept ': B' text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, image/apng, */*; q = 100 ', B 'accept-Language ': B' zh-CN, zh; q = 0.9 ',} def get_13_time (): return str (int (time. time () * 1000) class WXRobot (object): def _ init _ (self): self. s = requests. session () self. s. verify = False self. s. headers = DEFAULT_HEADERS self. deviceID = 'E' + str (random. random () [2: 17] def visit_index (self): url = 'https :// Wx.qq.com/'self. s. get (url) def visit_jslogin (self): url = 'https: // login.wx.qq.com/jslogin? Appid = signature & redirect_uri = https % 3A % 2F % 2Fwx.qq.com % 2Fcgi-bin % 2Fmmwebwx-bin % 2 Fwebwxnewloginpage & fun = new & lang = zh_CN & _= {}'. format (get_13_time () r = self. s. get (url) text = r. text maths = re. findall (r'window. QRLogin. code = 200; window. QRLogin. uuid = "(. *?) "; ', Text) [0] uuid = str (maths) self. uuid = uuid def visit_vcode (self): # obtain the verification code url = 'https: // login.weixin.qq.com/qrcode/{'.format (self. uuid) r = self. s. get (url) with open('qrcode.jpg ', 'wb') as f: f. write (r. content) f. flush () f. close () print ('waiting to scan the verification code... ') Def visit_login (self): url = 'https: // login.wx.qq.com/cgi-bin/mmwebwx-bin/login? Loginicon = true & uuid = {} & tip = 0 & _ = {}'. format (self. uuid, get_13_time () r = self. s. get (url) text = r. text code = re. findall (r'''window. code = (. *?); ''', Text) [0] # print ('get status Code', code) if str (code )! = '000000': # print ('call again in two seconds ') time. sleep (2) self. visit_login () elif str (code) = '000000': maths = re. findall (r'''redirect _ uri = "(. *?) "''', Text) [0] self. redirect_uri = maths print ('redirect _ uri retrieved... ', self. redirect_uri) # obtain the keyword def visit_newloginpage (self): r = self. s. get (self. redirect_uri, allow_redirects = False) text = r. text bs = BeautifulSoup (text, 'lxml') self. skey = bs. find ('skey '). text self. wxsid = bs. find ('wxsid '). text self. wxuin = bs. find ('wxuin '). text self. pass_ticket = bs. find ('pass _ ticket '). text self. isgrayscale = bs. find ('is Grayscale '). text # print (self. skey, self. wxsid, self. wxuin, self. pass_ticket) # obtain the initialization information def visit_webwxinit (self): url = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit? Pass_ticket = {}'. format (self. pass_ticket) data = {"BaseRequest": {"Uin": self. wxuin, "Sid": self. wxsid, "Skey": self. skey, "DeviceID": self. deviceID} r = self. s. post (url, json = data) r. encoding = 'utf-8' jsontext = r. json () # = json. loads (text) print ('login User: ', jsontext ["User"] ["NickName"]) self. fromUserName = jsontext ["User"] ["UserName"] self. group_list = jsontext ['contactlist'] # SyncKey = jsontext ["SyncKey"] ["List"] Ch AtSet = jsontext ["ChatSet"] ChatSetlist = str (ChatSet ). split (',') self. chatSet = [{"UserName": name, "EncryChatRoomId": ""} for name in ChatSetlist if name. startswith ('@')] def visit_policy (self): url = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify? Pass_ticket = {}'. format (self. pass_ticket) data = {"BaseRequest": {"Uin": self. wxuin, "Sid": self. wxsid, "Skey": self. skey, "DeviceID": self. deviceID}, "Code": 3, "FromUserName": self. fromUserName, "ToUserName": self. fromUserName, "ClientMsgId": int (get_13_time ()} r = self. s. post (url = url, json = data) r. encoding = 'utf-8' self. msgID = r. json () ['msgid'] print (r. json () ['msgid'], 'message id') # retrieve all friends list def getcontact (Self): url = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact? Pass_ticket ={}& r ={} & seq = 0 & skey = @{}'. format (self. pass_ticket, get_13_time (), self. skey) r = self. s. get (url) r. encoding = 'utf = 8' contactjson = r. json () # print (contactjson) self. memberList = contactjson ["MemberList"] for name in self. memberList: print (name ['nickname'], name ['username']) # chat list def batchgetcontact (self): url = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact? Type = ex & r ={}& pass_ticket = {}'. format (get_13_time (), self. pass_ticket) data = {"BaseRequest": {"Uin": self. wxuin, "Sid": self. wxsid, "Skey": self. skey, "DeviceID": self. deviceID}, "Count": len (self. chatSet), "List": self. chatSet} r = self. s. post (url = url, json = data) r. encoding = 'utf-8' ContactList = r. json () ["ContactList"] for I in ContactList: print ('chat window ---- ', I. get ('nickname') def get_local (self): return s Tr (time. time () + str (random. random () [2: 9] def send_msg (self): ToUserName = input ('Who are you going to send it? ') Msg = input ('Enter the information you want to send content') url = 'https: // wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg? Pass_ticket = {}'. format (self. pass_ticket) data = {"BaseRequest": {"Uin": self. wxuin, "Sid": self. wxsid, "Skey": self. skey, "DeviceID": self. deviceID}, "Msg": {"Type": 1, "Content": msg, "FromUserName": self. fromUserName, "ToUserName": ToUserName, "LocalID": self. get_local (), "ClientMsgId": self. get_local ()}, "Scene": 0} r = self. s. post (url = url, json = data) json_data = r. json () if 0 = json_data ['baseresponse'] ['ret ']: print ('message sent successfully') else: print ('message sent failed ') if _ name _ = '_ main _': wx = WXRobot () wx. visit_index () wx. visit_jslogin () wx. visit_vcode () wx. visit_login () wx. visit_newloginpage () wx. visit_webwxinit () wx. visit_policy () wx. getcontact () wx. batchgetcontact () while True: wx. send_msg () # wx. x () # Heartbeat packet # time. sleep (50)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.