Python自动抢购脚本
from selenium import webdriver
import time
import datetime
driver = webdriver.Firefox(executable_path = r'C:\Program Files\Mozilla Firefox\geckodriver.exe')
driver.get('https://cart.taobao.com/cart.htm?spm=a21bo.2017.1997525049.1.6f9a11d9KLuDhn&from=mini&ad_id=&am_id=&cm_id=&pm_id=1501036000a02c5c3739') #购物车地址
time.sleep(15)
interval = 0.1
last = 1800
elapse = 0
def click_button():
driver.find_element_by_xpath('//*[@id="J_Go"]').click()
#original xpath: /html/body/div[1]/div[2]/div[2]/div/div[4]/div[2]/div[3]/div[5]/a/span
def buy():
global elapse
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div/div/div/div/ul/li[1]/div/div/label').click()
#original xpath: //div[@class="cart-checkbox"]/lable[@for="J_CheckBox_1008935861307"
while elapse<last :
t = datetime.datetime.now()
now = t.strftime('%Y-%m-%d %H:%M:%S')
if now == '2019-7-26 13:13:00': #抢购时间
print('Start buying now!')
click_button()
break
time.sleep(interval)
elapse += interval
buy()
#附:需先从https://github.com/mozilla/geckodriver/releases下载geckdrive.exe,然后把它加入浏览器安装目录中(不一定),加入环境变量中(可选);
#需要下载第三方模块:selenium;通过copy元素的xpath来获取元素;为防止找不到元素,用sleep()等页面加载完毕再执行后续操作;登录淘宝要扫码;
20190726