python笔记(爬虫 微爬取微信信息)

views.py

import time
import json
import re
import requests
from bs4 import BeautifulSoup
from flask import Blueprint, render_template, session, jsonify, request


wx = Blueprint("wx", __name__)


def xml_parse(text):
    result = {}
    soup = BeautifulSoup(text, "html.parser")
    tag_list = soup.find(name="error").find_all()
    for tag in tag_list:
        result[tag.name] = tag.text
    return result


@wx.route("/login")
def login():
    ctime = int(time.time() * 1000)
    wcode_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(ctime)
    res = requests.get(
        url=wcode_url
    )
    data = res.text
    uuid = re.findall('uuid = "(.*)";', data)[0]
    session["uuid"] = uuid
    return render_template("wechat_page.html", uuid=uuid)


@wx.route("/check_login")
def check_login():
    uuid = session.get("uuid")
    ctime = int(time.time() * 1000)
    check_login_url = "https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={}&tip=0&r=-1916563215&_={}".format(uuid, ctime)
    res = requests.get(
        url=check_login_url
    )
    result = {"code": None}
    if "window.code=408" in res.text:
        result["code"] = 408
    elif "window.code=201" in res.text:
        result["code"] = 201
        result["userAvatar"] = re.findall("window.userAvatar = '(.*)';", res.text)[0]
    elif "window.code=200" in res.text:
        result["code"] = 200
        redirect_uri = re.findall('redirect_uri="(.*)";', res.text)[0]+"&fun=new&version=v2"
        ret = requests.get(
            url=redirect_uri
        )
        ticket_dict = xml_parse(ret.text)
        session["ticket_dict"] = ticket_dict
        session["ticket_cookie"] = ret.cookies.get_dict()
    return jsonify(result)


@wx.route("/mainpage")
def mainpage():
    pass_ticket = session["ticket_dict"].get("pass_ticket")
    init_url = " https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-1932693400&pass_ticket={}".format(pass_ticket)
    res = requests.post(
        url=init_url,
        json={
            "BaseRequest": {
                "DeviceID": "e145195217922834",
                "Sid": session["ticket_dict"].get("wxsid"),
                "Skey": session["ticket_dict"].get("skey"),
                "Uin": session["ticket_dict"].get("wxuin")
            }
        }
    )
    res.encoding = "utf-8"

    init_user_dict = res.json()
    return render_template("wechat_index.html", init_user_dict=init_user_dict)


@wx.route("/contractlist")
def contractlist():
    ctime = int(time.time() * 1000)
    # pass_ticket = session["ticket_dict"].get("pass_ticket")
    skey = session["ticket_dict"].get("skey")
    print(session.get("ticket_cookie"))
    contract_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?r={0}&seq=0&skey={1}".format(ctime, skey)
    res = requests.get(
        url=contract_url,
        cookies=session.get("ticket_cookie")
    )
    res.encoding = "utf-8"
    user_list = res.json()
    return render_template("contact_list.html", userlist=user_list.get("MemberList"))


@wx.route("/get_img")
def get_img():
    prev = request.args.get("prev")
    username = request.args.get("username")
    skey = request.args.get("skey")
    head_img_url = "https://wx2.qq.com{0}&username={1}&skey={2}".format(prev, username, skey)
    res = requests.get(
        url=head_img_url,
        cookies=session.get("ticket_cookie")
    )
    return res.content


@wx.route("/send_msg", methods=["POST"])
def send_msg():
    ctime = int(time.time() * 1000)
    from_user = request.form.get("fromuser")
    to_user = request.form.get("touser")
    content = request.form.get("content")

    data_dict = {
        "BaseRequest": {
            "DeviceID": "e823270257010524",
            "Sid": session["ticket_dict"].get("wxsid"),
            "Skey": session["ticket_dict"].get("skey"),
            "Uin": session["ticket_dict"].get("wxuin")
        },
        "Msg": {
            "ClientMsgId": ctime,
            "Content": content,
            "FromUserName": from_user,
            "LocalID": ctime,
            "ToUserName": to_user,
            'Type': 1
        },
        'Scene': 0
    }
    send_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN&pass_ticket={0}".format(session["ticket_dict"].get("pass_ticket"))
    ret = requests.post(
        url=send_url,
        data=bytes(json.dumps(data_dict, ensure_ascii=False), encoding="utf-8")
    )
    print(ret)
    return "发送成功"

wechat_page.html




    
    微信


扫码登录

python笔记(爬虫 微爬取微信信息)_第1张图片
python笔记(爬虫 微爬取微信信息)_第2张图片
wechat_index.html




    
    微信首页


欢迎来到微信{{init_user_dict.User.UserName}}

最近联系人

    {% for row in init_user_dict.ContactList %}
  • {{row.NickName}}
  • {% endfor %} 联系人列表

最近公众号

{% for item in init_user_dict.MPSubscribeMsgList %}

{{item.NickName}}

{% endfor %}

python笔记(爬虫 微爬取微信信息)_第3张图片

contact_list.html




    
    联系人列表


自己的唯一标识: 别人的唯一标识: 发送内容:

联系人列表

{% for user in userlist %}
  • 用户名:{{user.NickName}} 唯一标识:{{user.UserName}}
  • {% endfor %}

    python笔记(爬虫 微爬取微信信息)_第4张图片

    你可能感兴趣的:(爬虫)