python实现微信机器人 + 淘宝客自动领券(二)

原理概述

该系统采用的传统的cs架构,客户端和服务器的语言都是采用python写的。服务器采用的django框架部署在本地,主要用于自动登陆淘宝,阿里妈妈等平台采集信息,客户端通过hook微信,接受用户的消息,进行判断以及回复。

服务器端项目结构,省略掉django其他目录
其中核心代码为:
Login.py

import re
import os
import json

import requests

s = requests.Session()
# cookies序列化文件
COOKIES_FILE_PATH = 'taobao_login_cookies.txt'


class UsernameLogin:

    def __init__(self, username, ua, TPL_password2):
        """
        账号登录对象
        :param username: 用户名
        :param ua: 淘宝的ua参数
        :param TPL_password2: 加密后的密码
        """
        # 检测是否需要验证码的URL
        self.user_check_url = 'https://login.taobao.com/member/request_nick_check.do?_input_charset=utf-8'
        # 验证淘宝用户名密码URL
        self.verify_password_url = "https://login.taobao.com/member/login.jhtml"
        # 访问st码URL
        self.vst_url = 'https://login.taobao.com/member/vst.htm?st={}'
        # 淘宝个人 主页
        self.my_taobao_url = 'http://i.taobao.com/my_taobao.htm'

        # 淘宝用户名
        self.username = username
        # 淘宝关键参数,包含用户浏览器等一些信息,很多地方会使用,从浏览器或抓包工具中复制,可重复使用
        self.ua = ua
        # 加密后的密码,从浏览器或抓包工具中复制,可重复使用
        self.TPL_password2 = TPL_password2

        # 请求超时时间
        self.timeout = 3

    def _user_check(self):
        """
        检测账号是否需要验证码
        :return:
        """
        data = {
            'username': self.username,
            'ua': self.ua
        }
        try:
            response = s.post(self.user_check_url, data=data, timeout=self.timeout)
            response.raise_for_status()
        except Exception as e:
            print('检测是否需要验证码请求失败,原因:')
            raise e
        needcode = response.json()['needcode']
        print('是否需要滑块验证:{}'.format(needcode))
        return needcode

    def _verify_password(self):
        """
        验证用户名密码,并获取st码申请URL
        :return: 验证成功返回st码申请地址
        """
        verify_password_headers = {
            'Connection': 'keep-alive',
            'Cache-Control': 'max-age=0',
            'Origin': 'https://login.taobao.com',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': 'https://login.taobao.com/member/login.jhtml?from=taobaoindex&f=top&style=&sub=true&redirect_url=https%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm',
        }
        # 登录toabao.com提交的数据,如果登录失败,可以从浏览器复制你的form data
        verify_password_data = {
            'TPL_username': self.username,
            'ncoToken': 'cdf05a89ad5104403ebb12ebc9b7626af277b066',
            'slideCodeShow': 'false',
            'useMobile': 'false',
            'lang': 'zh_CN',
            'loginsite': 0,
            'newlogin': 0,
            'TPL_redirect_url': 'https://s.taobao.com/search?q=%E9%80%9F%E5%BA%A6%E9%80%9F%E5%BA%A6&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306',
            'from': 'tb',
            'fc': 'default',
            'style': 'default',
            'keyLogin': 'false',
            'qrLogin': 'true',
            'newMini': 'false',
            'newMini2': 'false',
            'loginType': '3',
            'gvfdcname': '10',
            'gvfdcre': '68747470733A2F2F6C6F67696E2E74616F62616F2E636F6D2F6D656D6265722F6C6F676F75742E6A68746D6C3F73706D3D61323330722E312E3735343839343433372E372E33353836363032633279704A767526663D746F70266F75743D7472756526726564697265637455524C3D6874747073253341253246253246732E74616F62616F2E636F6D25324673656172636825334671253344253235453925323538302532353946253235453525323542412532354136253235453925323538302532353946253235453525323542412532354136253236696D6766696C65253344253236636F6D6D656E64253344616C6C2532367373696425334473352D652532367365617263685F747970652533446974656D253236736F75726365496425334474622E696E64657825323673706D253344613231626F2E323031372E3230313835362D74616F62616F2D6974656D2E31253236696525334475746638253236696E69746961746976655F69642533447462696E6465787A5F3230313730333036',
            'TPL_password_2': self.TPL_password2,
            'loginASR': '1',
            'loginASRSuc': '1',
            'oslanguage': 'zh-CN',
            'sr': '1440*900',
            'osVer': 'macos|10.145',
            'naviVer': 'chrome|76.038091',
            'osACN': 'Mozilla',
            'osAV': '5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
            'osPF': 'MacIntel',
            'appkey': '00000000',
            'mobileLoginLink': 'https://login.taobao.com/member/login.jhtml?redirectURL=https://s.taobao.com/search?q=%E9%80%9F%E5%BA%A6%E9%80%9F%E5%BA%A6&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&useMobile=true',
            'showAssistantLink': '',
            'um_token': 'T898C0FDF1A3CEE5389D682340C5F299FFE590F51543C8E3DDA8341C869',
            'ua': self.ua
        }
        try:
            response = s.post(self.verify_password_url, headers=verify_password_headers, data=verify_password_data,
                              timeout=self.timeout)
            response.raise_for_status()
            # 从返回的页面中提取申请st码地址
        except Exception as e:
            print('验证用户名和密码请求失败,原因:')
            raise e
        # 提取申请st码url
        apply_st_url_match = re.search(r'<script src="(.*?)"></script>', response.text)
        # 存在则返回
        if apply_st_url_match:
            print('验证用户名密码成功,st码申请地址:{}'.format(apply_st_url_match.group(1)))
            return apply_st_url_match.group(1)
        else:
            raise RuntimeError('用户名密码验证失败!response:{}'.format(response.text))

    def _apply_st(self):
        """
        申请st码
        :return: st码
        """
        apply_st_url = self._verify_password()
        try:
            response = s.get(apply_st_url)
            response.raise_for_status()
        except Exception as e:
            print('申请st码请求失败,原因:')
            raise e
        st_match = re.search(r'"data":{"st":"(.*?)"}', response.text)
        if st_match:
            print('获取st码成功,st码:{}'.format(st_match.group(1)))
            return st_match.group(1)
        else:
            raise RuntimeError('获取st码失败!response:{}'.format(response.text))

    def login(self):
        """
        使用st码登录
        :return:
        """
        # 加载cookies文件
        if self._load_cookies():
            return True
        # 判断是否需要滑块验证
        self._user_check()
        st = self._apply_st()
        headers = {
            'Host': 'login.taobao.com',
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
        }
        try:
            response = s.get(self.vst_url.format(st), headers=headers)
            response.raise_for_status()
        except Exception as e:
            print('st码登录请求,原因:')
            raise e
        # 登录成功,提取跳转淘宝用户主页url
        my_taobao_match = re.search(r'top.location.href = "(.*?)"', response.text)
        if my_taobao_match:
            print('登录淘宝成功,跳转链接:{}'.format(my_taobao_match.group(1)))
            self._serialization_cookies()
            return True
        else:
            raise RuntimeError('登录失败!response:{}'.format(response.text))

    def _load_cookies(self):
        # 1、判断cookies序列化文件是否存在
        if not os.path.exists(COOKIES_FILE_PATH):
            return False
        # 2、加载cookies
        s.cookies = self._deserialization_cookies()
        # 3、判断cookies是否过期
        try:
            self.get_taobao_nick_name()
        except Exception as e:
            os.remove(COOKIES_FILE_PATH)
            print('cookies过期,删除cookies文件!')
            return False
        print('加载淘宝登录cookies成功!!!')
        return True

    def _serialization_cookies(self):
        """
        序列化cookies
        :return:
        """
        cookies_dict = requests.utils.dict_from_cookiejar(s.cookies)
        with open(COOKIES_FILE_PATH, 'w+', encoding='utf-8') as file:
            json.dump(cookies_dict, file)
            print('保存cookies文件成功!')

    def _deserialization_cookies(self):
        """
        反序列化cookies
        :return:
        """
        with open(COOKIES_FILE_PATH, 'r+', encoding='utf-8') as file:
            cookies_dict = json.load(file)
            cookies = requests.utils.cookiejar_from_dict(cookies_dict)
            return cookies

    def get_taobao_nick_name(self):
        """
        获取淘宝昵称
        :return: 淘宝昵称
        """
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
        }
        try:
            response = s.get(self.my_taobao_url, headers=headers)
            response.raise_for_status()
        except Exception as e:
            print('获取淘宝主页请求失败!原因:')
            raise e
        # 提取淘宝昵称
        nick_name_match = re.search(r'<input id="mtb-nickname" type="hidden" value="(.*?)"/>', response.text)
        if nick_name_match:
            print('登录淘宝成功,你的用户名是:{}'.format(nick_name_match.group(1)))
            return nick_name_match.group(1)
        else:
            raise RuntimeError('获取淘宝昵称失败!response:{}'.format(response.text))


if __name__ == '__main__':
    # 淘宝用户名
    username = '你的淘宝账号'
    # 淘宝重要参数,从浏览器或抓包工具中复制,可重复使用
    ua = '119#Ml88qzses2GAoMMzEyFDngNzxSb0aF0jvLJis4SPegM+zO/SrpJmpXMemUBl9S9S2MxpMrnoOrOPEdmp49I4nuxDDsmz42k2MVJW3CkL9U+S4lkGqr32S/0QNEKCFo3T4BKUzs5L/U+g4NeVqrAFWs+XnRbGfoAzRBSedFN89COb/lFLdA7MMOT48oxm5rA1RZeedtNnfU+E471y0ZA8J5BVN88L9eFqwPsUcV58YUot19H7I0rORJVVNEFkfHDgsBB2XtN8gpV8zly3A3E8RmKVNEHT9dAzeIeUspq8fU+S4QR73eALR2PQNs829oASRBSUstN8fUVl4TLLdHR5R26ONNFLMsMXqmBhxiLmXUEbqCaZSCqOfoDGn9jm1VChfC0IYWImgkr3TXv97EmqlkrryajB6Sc4xGvCtn1i0TQSHqH9U//xgEcik+qYs0Hz6F05aod24o5E5w9R/y+SOzuzpj/2LFLbbKMir5vYriyUElW9jJRB3lWl29J1IzE9AkmXQUKvK17xi8m+XMfa/nQOqjNw+9xQ/shUYXNWrdSzn/p4aqAwyVAesSfwrTEM4CoYv8l3G4h6eHXfqkIkI5umAIT/IWyIB1h8pTUszi89IuJEgUnBrn99IzRkSj+LuzojDYzarl5Nz5zCpfO0245KbesCYq/0F7XJhMms61z1rXDvHDJaIsoPxa4WWGgoAp8S1Mt/TWfUw/DBADQgXiBxZFpvM7TyfhxOpP+ItvGnJoDu/9OPzZ8u3VVvskBJGoA1EKT63U+A5sHSlkiweFeprMShrW4mdB8iV2mzkiIxfA7pGDJ6j82UWYtEBPjDbhmNMkMI1IV21vr8X7FZJAkVyBE9+HflkWHTnOZ13/+pjSFyLenTZwuCN9AZbsTBiDWQrw0EFzjQhbo/stzKDBWcyxzdhseP5oQWiZ6xozpV+bLrCuxUORDVkG+x7u2Icp6cVby/xTzpiVKFDZY9buOh3EwsWbDmL9YqEIXMpT+7GBM3jzKjM4RFaAyWJiBbj3UH22eWGIeF61BvnveW2+tPnGq3lCVNF1nHLRvUX3riCkP8N6V+J4StRjZHkMtsU3eZVA2FSKSt0RQ5u45RuiiavvomikyniQgYmLNz57wjrAuLiI3616bPQ6i+FhXoJQhHsIfZoyCukdsL2UlytWm4RPXY+hLaZ2sUMUDO5W9Un2ztnVpprKR/i8tTO7Qy9XevLmVtRe0lkiuDhkJg9URp5I3RvWJbadogH5G2WhocskZhzPEDtiT9xSdR9JSshUmq44/68QbxWrCvYMf9/9M1UGa33zAMcWxhG7xRlPUGG6kPik4ZeOMKoXR2uUWO1ozII2PpbhTls8mte+WYzqjE/cnip5m8FpRIul+BfrO95ybXTzm8j6j+mr8szHx1kiTQUwypyNm7CrxnA5nYdlZRbb26GfgTqtMgzr4xSOwEJroUKMBIWThFBs8OUTzoQEXF9yK6LwVsYyqqXt8dklFWWmZN5p05BIgH1Z5Ia+Eq3R8ER5BU2XjjA4pqvf7kBe7PfEos5O15ZmfQddX+j0MyKi46+sasyy=='
    # 加密后的密码,从浏览器或抓包工具中复制,可重复使用
    TPL_password2 = '你的加密后密码'
    ul = UsernameLogin(username, ua, TPL_password2)
    ul.login()

至于为什么是这样写的,如果有兴趣的小伙伴可以回复留言,人数多的话,我另开一篇文章讲解淘宝的抓包技术。
摘自:王惠亮的个人博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值