一段时间前,微信群里有小伙伴在问如何模拟登陆淘宝。对于这样的需求我很乐意折腾,我也在网上看了一些大神写的,不用第三方组件,“纯模拟“实现的难度太大了,各种参数,可见淘宝安全机制且高。学艺不精的我,思想太简单我就用简单的方式实现了,目前还有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.pypip 下载慢可直接用浏览器访问另存为,然后继续执行下面的python get-pip.py2.2.2 通过 pip 安装 seleniumpip 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 urllibimport urllib2import cookielibimport reimport webbrowserimport jsonimport sysimport timeimport osfrom selenium.webdriver.common.proxy import *from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keys#模拟登录淘宝类class Taobao:#初始化方法def __init__(self):#登录的URLself.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"#检查是否需要滑块解锁的URLself.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 全局 tokenself._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'获取用户消息失败!'  #登录获取cookiedef 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#监测是否需要滑动解锁 tododef 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也没有问题)

python+selenium+phantomjs 模拟淘宝登陆相关推荐

  1. python模拟淘宝登陆_【Python】selenium模拟淘宝登录

    # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By f ...

  2. python+selenium+chrome实现淘宝购物车秒杀自动结算

    python+selenium+chrome实现淘宝购物车秒杀自动结算 一.所需环境 二.安装 三.代码 最后run()一把就ok了!! 之前总是想要买aj,但是淘宝店铺每次发售手动抢的时候一般都会被 ...

  3. Python + selenium 爬取淘宝商品列表及商品评论 2021-08-26

    Python + selenium 爬取淘宝商品列表及商品评论[2021-08-26] 主要内容 登录淘宝 获取商品列表 获取评论信息 存入数据库 需要提醒 主要内容 通过python3.8+ sel ...

  4. Python selenium模拟淘宝登陆

    通过selenium定位到各个元素,通过相关操作(如输入框的输入,按钮的点击等)来模拟人的真实操作流程 流程是通过绕个圈子,用微博账号登陆来绕过淘宝登陆,不过事先需要将微博账号与淘宝账号进行绑定 下面 ...

  5. selenium模拟淘宝登陆

    使用selenium打开浏览器,模拟人工进行淘宝的登陆活动可以在网上找到两种解决方案. 第一种方案是打开网站后,通过find_element_by_xpath 依次寻找用户名录入框,密码录入框,登陆按 ...

  6. python +selenium 爬取淘宝网商品信息

    前几天用python爬取豆瓣关于电影<长城>的影评,发现豆瓣的网页是静态的,心中一阵窃喜.以为对于动态网页了解的不是太多.但是主要是用cookie加headers爬取的.效果还不错,爬取了 ...

  7. python+selenium爬取淘宝商品信息+淘宝自动登录——爬虫实战

    1.前言 继续学习爬虫内容,这回是以selenium模拟操作进行抓取,其中有几个需要登陆的注意事项. 2.自动登陆+查找页面 由于现在淘宝的反爬机制,需要登陆才好下一步操作.在搜索输入后页面会调入登陆 ...

  8. Python selenium 爬取淘宝商品

    Catalog 翻页 获取动态页面信息 提取商品信息 完整代码 翻页 from urllib import parse domain = 'https://s.taobao.com/search?' ...

  9. 吃货们看好了!python+selenium爬取淘宝美食

    前言 今天为大家介绍一个利用Python的selenium打开浏览器的方式来爬去淘宝美食,这个小案例涵盖了selenium的基本知识点,如何打开浏览器以及关键字的搜索 导入第三方库 打开浏览器 搜索功 ...

最新文章

  1. 在Ubuntu 14.04 64bit上使用网络诊断工具mtr
  2. 【python】nuitka封装python
  3. python怎样在已有excel文件指定行写入-Python中使用第三方库xlrd来写入Excel文件示例...
  4. Opera 扩展文章目录
  5. Oracle Data Gurad Physical Standby 相关说明
  6. repeater导出excel html,Repeater显示数据,并且导出到excel
  7. APUE第二版源码编译问题解决
  8. Linux 下非 root 用户安装 theano(配置 GPU)
  9. 与计算机专业相关的英语科普短文,英语科普文选-中英文对照(计算机.doc
  10. html 数据库 编写学生表,【数据库】用sql语句创建学生表如何做
  11. OEM、ODM 、OBM分别是什么意思?
  12. upload上传, 取各类文件的名字
  13. Instrument初识
  14. 【网格压缩测评】MeshQuan、MeshOpt、Draco
  15. JavaScript的document对象详解
  16. Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛
  17. 最全最新的的Java核心知识点整理!!! 【推荐】
  18. sap crm button_如何安装SAP软件?
  19. 全球与中国汽车牛皮革市场“十四五”前景规划及运营趋势研究报告2022-2028年
  20. 计算机excel公式2010,计算机二级Office2010Eexcel公式汇总

热门文章

  1. [IT最前沿--有点强大] 都市圈实时公交:一款实时查询公交到站信息的应用
  2. 7N60-ASEMI场效应管7N60
  3. 基于sklearn随机森林算法对鸢尾花数据进行分类
  4. linux 安装rar
  5. 京东数据化运营(五)— 行业分析篇
  6. linux系统virtualenv创建虚拟环境提示command not found
  7. 发明专利申请流程与条件
  8. 单招计算机面试技巧和注意事项,单招面试有哪些技巧和注意事项
  9. java 时间戳 周几_Java:Unix时间戳记中的日期
  10. 如何通过知识星球粉丝变现年入100万?