python模拟登录练习(三)

经历过模拟登录微博之后,今天试了一下爬网页版的微信,感觉比微博简单多了。

import requests
import time
import re
from PIL import Image


tip = 1
session = requests.session()
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
def getuuid():
    global uuid
    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&_='\
    +str(int(time.time()))
    data = session.get(url,headers=headers)
    #print(data.text)

    #获得code和uuid的值
    q = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"'
    p = re.search(q,data.text)
    code = p.group(1)
    uuid = p.group(2)
    #print(code,uuid)

    if code == '200':
        return True
    else:
        return False

def showImage():
    url = 'https://login.weixin.qq.com/qrcode/%s'% uuid

    #保存二维码图片
    with open('二维码.jpg','wb') as f:
        f.write(session.get(url).content)

    im = Image.open('二维码.jpg')
    im.show()
    im.close()

    print('请扫描二维码')

def Islogin():
    global tip
    url = 'https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s'% (tip,uuid,int(time.time()))
    data = session.get(url)
    q = r'window.code=(\d+);'
    code = re.findall(q,data.text)[0]

    if code == '201':
        print('扫描成功,请点击确认')
        tip = 0
    elif code == '200':
        print('正在登录~~~')
        print(data.text)
    else:
        print('登录失败')
        print(code)
    return code

def mian():
    getuuid()
    showImage()
    while Islogin() != '200':
        pass
if __name__=='__main__':
    print('开始')
    mian()

你可能感兴趣的:(python模拟登录练习(三))