前言
无意在论坛刷到了基于Python实现淘宝秒杀的小demo,觉得挺有意思的,就用Python写了一下
# 淘宝清空购物车装置
import time
# 新版的selenium和老版的有区别,坑死我了
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
ShoppingTime = "19:00"
# 先登录
driver.get("https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F/")
time.sleep(15)
# 进入购物车
driver.get("https://cart.taobao.com/cart.htm?from=mini&ad_id=&am_id=&cm_id=&pm_id=1501036000a02c5c3739")
time.sleep(2)
# 选中全选
all = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div/div/div[2]/div[1]/div/div['
'1]/div/div/label')
if all :
all.click()
time.sleep(1)
buy = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div/div/div[4]/div[2]/div[3]/div[5]/a')
后来写时间判断的时候发现时间格式要转来转去的,Python是真的不熟,想到openqa好像也有selenium,就试一下用Java去写,简单搜了一下用Java写这个的真不多,自己折腾一下咯,不过涉及到爬虫还是推荐用Python去当主语言写,库多教程也多,少走弯路。
第一步 安装ChromeDriver
默认大家都用Chrome咯,下载地址:
https://sites.google.com/a/chromium.org/chromedriver/home
因为我是mac环境,下好chromedriver之后直接拷贝在/usr/local/bin下就行啦
拷贝的时候注意需要管理员权限,按个指纹就完了
也可以放在其他位置,写Java的时候用代码配一下就行
System.setProperty("webdriver.chrome.driver", "/Users/beamstark/Desktop/chromedriver");
第二步 导包
默认大家都用Maven
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<