python+selenium+phantomjs 模拟淘宝登陆

来源:互联网 发布:mac itunes 设置铃声 编辑:程序博客网 时间:2024/04/30 09:51

一段时间前,微信群里有小伙伴在问如何模拟登陆淘宝。对于这样的需求我很乐意折腾,我也在网上看了一些大神写的,不用第三方组件,“纯模拟“实现的难度太大了,各种参数,可见淘宝安全机制且高。学艺不精的我,思想太简单我就用简单的方式实现了,目前还有2个问题没解决,一个是登录时候滑动解锁这块,这块也能办到,还有淘宝的安全验证机制这块,可能需要发送验证码至手机,这块需要模拟触发,然后手机收到验证码再和自己的程序交互,都是能解决的。且在windows linux 下都是能运行的,我也只是实现了基本,大牛绕道勿喷。

环境要求

  1. 系统要求

    demo 示例在 centos 6.5下运行

    linux mac windows, linux mac 建议使用当前主流版本

  2. 脚本依赖要求

    2.1 python 2.7 ~ python 3

    2.2 python 脚本依赖

    2.2.1 pip     wget https://bootstrap.pypa.io/get-pip.py    pip 下载慢可直接用浏览器访问另存为,然后继续执行下面的    python get-pip.py2.2.2 通过 pip 安装 selenium    pip install -U selenium

    2.3 安装 PhantomJS( centos6.5 为例)

    yum -y install wget fontconfig wget -P /tmp/ https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-i686.tar.bz2PhantomJS 备用下载地址: http://pan.baidu.com/s/1gfKAy0ztar xjf /tmp/phantomjs-2.1.1-linux-i686.tar.bz2 -C /usr/local/mv /usr/local/phantomjs-2.1.1-linux-i686 /usr/local/phantomjsln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/

基础小学生代码,简单明了

```    #coding=UTF-8    ###    # 脚本依赖:    # python 2.7 以上,3.0 一以下 https://www.python.org/ftp/python/2.7.9/    # selenium 2.5    #    ###    __author__ = 'Kevin'    import urllib    import urllib2    import cookielib    import re    import webbrowser    import json    import sys    import time    import os    from selenium.webdriver.common.proxy import *    from selenium import webdriver    from selenium.webdriver.common.action_chains import ActionChains    from selenium.webdriver.common.keys import Keys    #模拟登录淘宝类    class Taobao:        #初始化方法        def __init__(self):            #登录的URL            self.loginURL = "https://login.taobao.com/member/login.jhtml?style=mini&newMini2=true&css_style=alimama&from=alimama&redirectURL=http%3A%2F%2Fwww.alimama.com&full_redirect=true&disableQuickLogin=true"            #检查是否需要滑块解锁的URL            self.needCodeURL = "https://login.taobao.com/member/request_nick_check.do?_input_charset=utf-8"            #用户消息中心            self.accountInfoURL = "http://ad.alimama.com/earned/settle/getAccountInfo.json"            self.TPL_username = '841694874@qq.com'            self.TPL_password = 'your taobao account password'            self.service_args = [                #'--proxy=218.241.30.187:8123',                #'--proxy-type=http',            ]            self.driver = webdriver.PhantomJS(executable_path = "D:\\python\\phantomjs\\bin\\phantomjs.exe", service_args=self.service_args)            self.driver.set_window_size(1920,1080)            #代理IP地址,防止自己的IP被封禁            #self.proxyURL = 'http://120.193.146.97:843'            #登录POST数据时发送的头部信息            self.loginHeaders =  {                'Host':'login.taobao.com',                'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0',                'Referer' : 'https://login.taobao.com/member/login.jhtml',                'Content-Type': 'application/x-www-form-urlencoded',                'Connection' : 'Keep-Alive'            },            #保存一个 taobao 全局 token            self._tb_token_ = ''        #获取用户消息        def getAccountInfo(self, cookiestr):            postData = urllib.urlencode({                'startTime':'2016-11-09',                'endTime':'2016-11-16',                '_tb_token_':self._tb_token_            })            print u'登录时候的Token:',self._tb_token_            headers = {'cookie':cookiestr}            req = urllib2.Request(self.accountInfoURL, postData, headers = headers)            try:                  response = urllib2.urlopen(req)                  content = response.read()                  filename = './userMessage.html'                self.saveFile(content, filename)                print u'已经获取到账户内容:',content              except:                  print u'获取用户消息失败!'          #登录获取cookie        def login(self):            self.driver.get(self.loginURL)            time.sleep(3)            self.switchFromLogin()            self.inputUserName()            self.inputPassword()            self.driver.find_element_by_id("J_SubmitStatic").click()            time.sleep(1)            cookie = self.driver.get_cookies()            cookiefilepath = './userCookie.txt'            cookiestr = self.saveCookie(cookie, cookiefilepath)            self.driver.close()            return cookiestr        #监测是否需要滑动解锁 todo        def needCode(self):            return False        #切换普通表单登陆        def switchFromLogin(self):            self.driver.find_element_by_id("J_Quick2Static").click()        def inputUserName(self):            user_name = self.driver.find_element_by_id("TPL_username_1")            user_name.clear()            user_name.send_keys(self.TPL_username)        def inputPassword(self):            password = self.driver.find_element_by_id("TPL_password_1")            password.clear()            password.send_keys(self.TPL_password)        #cookie 写入本地,利于查看,且可返回cookies string          def saveCookie(self, cookies, cookfilepath):            cookie = []            for item in cookies:                if(item["name"] == '_tb_token_'):                    self._tb_token_ = item["value"]            cookie = [item["name"] + "=" + item["value"] for item in cookies]              cookiestr = ';'.join(item for item in cookie)            f = open(cookfilepath, "a+")            f.write(cookiestr)            f.close()            return cookiestr        #临时写入文件 利于调试            def saveFile(self, content, filepath):            f = open(filepath, "a+")            f.write(content)            f.close()        def main(self):            reload(sys)            sys.setdefaultencoding('utf-8')            cookfilepath = self.login();            self.getAccountInfo(cookfilepath)    taobao = Taobao()    taobao.main()```

运行(结果在windows下运行的,linux也没有问题)
模拟登陆淘宝,查询阿里妈妈账户信息

原创粉丝点击