亲宝软件园·资讯

展开

python模仿网页版微信发送消息 python模仿网页版微信发送消息功能

赵韶晖_ 人气:1
想了解python模仿网页版微信发送消息功能的相关内容吗,赵韶晖_在本文为您仔细讲解python模仿网页版微信发送消息的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python微信发送消息,python网页版微信发送消息,python微信发送,下面大家一起来学习吧。

这个微信版网页版虽然繁琐,但是不是很难,全程不带加密的。有兴趣的可以试着玩一玩,如果有兴趣的话,可以完善一下,做一些比较有意思的东西。

开发环境:Windows10
开发语言:Python3.6
开发工具:pycharm
抓包工具:fiddler

抓的包如下:

import requests
import time
import re
from bs4 import BeautifulSoup
import json
import random
from copyheaders import headers_raw_to_dict
DEFAULT_HEADERS={
  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=0.8',
  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=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&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):
    #获取验证码
    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('等待扫描验证码。。。')
  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('获取状态码', code)
    if str(code)!='200':
      # print('两秒后重新调用')
      time.sleep(2)
      self.visit_login()
    elif str(code)=='200':
      maths = re.findall(r'''redirect_uri="(.*?)"''', text)[0]
      self.redirect_uri=maths
      print('redirect_uri获取完成...',self.redirect_uri)

  #得到一些关键字
  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('isgrayscale').text
    # print(self.skey,self.wxsid,self.wxuin,self.pass_ticket)


  #得到初始化信息
  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('登录用户为:',jsontext["User"]["NickName"])
    self.FromUserName=jsontext["User"]["UserName"]
    self.group_list=jsontext['ContactList'] #
    SyncKey=jsontext["SyncKey"]["List"]
    ChatSet=jsontext["ChatSet"]
    ChatSetlist=str(ChatSet).split(',')
    self.ChatSet =[{"UserName":name ,"EncryChatRoomId":""} for name in ChatSetlist if name.startswith('@')]


  def visit_notify(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'],'消息id')
  #获取所有好友列表
  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'])

  # 聊天列表
  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('聊天窗口----',i.get('NickName'))

  def get_local(self):
    return str(time.time())+str(random.random())[2:9]
  def send_msg(self):

    ToUserName=input('你要发送给谁?')
    msg = input('请输入你要发送的信息内容')
    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('消息发送成功')
    else:
      print('消息发送失败')
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_notify()
  wx.getcontact()
  wx.batchgetcontact()
  while True:
    wx.send_msg()
  # wx.x() #心跳包
  # time.sleep(50)

加载全部内容

相关教程
猜你喜欢
用户评论