鱼C论坛

 找回密码
 立即注册
查看: 1544|回复: 1

[作品展示] 使用selenium实现淘宝抢购

[复制链接]
发表于 2020-3-30 00:27:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 啦啦啦13 于 2020-3-30 00:33 编辑
  1. from selenium import webdriver
  2. from time import sleep
  3. import time,datetime


  4. def login():
  5.     driver.get('https://www.taobao.com/')
  6.     if driver.find_element_by_xpath('//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]'):
  7.         driver.find_element_by_xpath('//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]').click()
  8.         print('请在十秒内完成扫码')
  9.         sleep(15)
  10.         driver.get('https://cart.taobao.com/cart.htm?spm=a1z02.1.a2109.d1000367.2e33782djMy9S0&nekot=1470211439694')
  11.     now = datetime.datetime.now()
  12.     print('登录成功,当前时间为',now.strftime('%Y-%m-%d %H:%M:%S'))  #strftime接收以时间元组,并返回以可读字符串

  13. def buy(times):
  14.     while True:
  15.         now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
  16.         if now > times:
  17.             while True:
  18.                 try:
  19.                     if driver.find_element_by_xpath('//*[@id="J_SelectAll1"]/div/label'):
  20.                         driver.find_element_by_xpath('//*[@id="J_SelectAll1"]/div/label').click()
  21.                         break
  22.                 except:
  23.                     print('找不到全选按钮!')
  24.             sleep(0.01)    #有时候运行过快,同一个页面可能会有BUG,程序以为点了结算,实际没点
  25.             while True:
  26.                 try:
  27.                     if driver.find_element_by_xpath('//*[@id="J_Go"]'):
  28.                         driver.find_element_by_xpath('//*[@id="J_Go"]').click()
  29.                         break
  30.                 except:
  31.                     print('找不到结算按钮!')
  32.             while True:
  33.                 try:
  34.                     if driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]'):           
  35.                         driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]').click()
  36.                         break
  37.                 except:
  38.                     print('找不到提交订单按钮!')   

  39. if __name__ == '__main__':
  40.     times = input('请输入抢购时间,格式为:2019-06-10 20:57:07.429161')
  41.     driver = webdriver.Chrome(r"C:\Users\A\Desktop\淘宝抢购\chromedriver.exe")   #驱动chromedriver.exe地址
  42.     login()
  43.     buy(times)
复制代码


附加该模块文档(转载)http://www.testclass.net/selenium_python
浏览器驱动https://sites.google.com/a/chromium.org/chromedriver/
这里我用的是谷歌,并注意版本号要一致
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-30 19:31:01 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-17 10:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表