python模拟登录微信网页版发送信息

import json
import random
import time
from PIL import Image
from bs4 import BeautifulSoup
import re
import requests
first_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&_=1520435074622'
captcha_url = 'https://login.weixin.qq.com/qrcode/{uuid}'
login_url = 'https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={uuid}&tip=0&r=-16664904&_={timestamp}'
login_url2 = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-16669464'
list_url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?r={timestamp}&seq=0&skey={skey}'
message_url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN&pass_ticket={passticket}'


class WX(object):
   def __init__(self):
       self.s = requests.session()
       self.s.verify = False
       self.uuid = ''
       self.skey = ''
       self.wxsid = ''
       self.wxuin = ''
       self.pass_ticket = ''

   def first_req(self):
       r = self.s.get(first_url)
       uuid = re.findall('uuid = "(.*?)"', r.text)[0]
       print(uuid)
       self.uuid = uuid

   def get_captcha(self):
       uuid = self.uuid
       r = self.s.get(captcha_url.format(uuid=uuid))
       with open('cap.jpg', 'wb') as f:
           f.write(r.content)

       img = Image.open('cap.jpg')
       assert isinstance(img, Image.Image)
       img.show()
       input()
       print('扫码完成')

   def login(self):
       uuid = self.uuid
       r = self.s.get(login_url.format(uuid=uuid, timestamp=int(time.time())))
       redirect_url = re.findall('window.redirect_uri="(.*?)"',r.text)[0]
       print(redirect_url)
       return redirect_url

   def get_keys_and_login(self):
       r_url = self.login()
       r = self.s.get(r_url, allow_redirects=False)
       print(r.text)
       soup = BeautifulSoup(r.text, 'lxml')
       skey = soup.find_all('skey')[0].text
       wxsid = soup.find_all('wxsid')[0].text
       wxuin = soup.find_all('wxuin')[0].text
       pass_ticket = soup.find_all('pass_ticket')[0].text
       isgrayscale = soup.find_all('isgrayscale')[0].text
       self.skey = skey
       self.wxsid = wxsid
       self.wxuin = wxuin
       self.pass_ticket = pass_ticket
       data = {
           "BaseRequest": {
               "Uin": wxuin,
               "Sid": wxsid,
               "Skey": "",
               "DeviceID": "e560479825088368"
           }
       }
       r = self.s.post(login_url2, data=json.dumps(data))
       r.encoding = 'utf-8'
       # print(r.text)
       self.FromUserName = r.json()['User']['UserName']

   def get_my_list(self):
       n2u = {}
       r = self.s.get(list_url.format(timestamp=time.time(), skey=self.skey))
       r.encoding = 'utf-8'
       print(r.json())
       for item in r.json()['MemberList']:
           n2u[item['NickName']] = item['UserName']
       return n2u

   @staticmethod
   def get_local():
       return str(time.time()) + str(random.random())[2:9]

   def send_message(self):
       n2u = self.get_my_list()
       content = input('message detail:')
       to_user = input('who you want to send?')
       ToUserName = n2u[to_user]
       data = {
               "BaseRequest": {
                   "Uin": self.wxuin,
                   "Sid": self.wxsid,
                   "Skey": self.skey,
                   "DeviceID": "e560479825088368"
               },
               "Msg": {
                   "Type": 1,
                   "Content": content,
                   "FromUserName": self.FromUserName,
                   "ToUserName": ToUserName,
                   "LocalID": self.get_local(),
                   "ClientMsgId": self.get_local()
               },
               "Scene": 0
           }
       self.s.post(message_url.format(passticket=self.pass_ticket), data=json.dumps(data))
       print('发送成功')

if __name__ == '__main__':
   w = WX()
   w.first_req()
   w.get_captcha()
   w.get_keys_and_login()
   w.send_message()

你可能感兴趣的:(python模拟登录微信网页版发送信息)