This article mainly introduces the ideas and codes of using the seleium module for Taobao seckill. If you need it, let’s learn together with the editor!
general idea
本文通过用python导入seleium库(selenium是Web的自动化测试工具),使电脑模拟人工操作chrom浏览器,自动化操作淘宝页面,根据抢购时间与系统时间对比从而实现淘宝秒杀!
Environment configuration
1. python3
2. pycharm
3. Python library: datetime, time, selenium, win32com.client
4. Google driver: chromedriver.exe (Google driver needs to be consistent with the current Google version of the system)
code display
#编写者:cwt
#时间:2022/6/5 15:30
import datetime #datetime比time高级了不少,可以理解为datetime基于time进行了封装,提供了更多实用的函数
import time
from selenium import webdriver #selenium是Web的自动化测试工具,webDriver 以本地化方式驱动浏览器
from selenium.webdriver.common.by import By
import win32com.client #python -m pip install pypiwin32 在命令行输入前面的代码之后,即可成功安装win32com包
speaker = win32com.client.Dispatch("SAPI.SpVoice")
times='2022-06-07 08:27:00' #抢购时间
driver=webdriver.Chrome() #驱动chrome浏览器
driver.get('https://www.taobao.com/') #进入淘宝网
time.sleep(3) #休息3秒等待网页刷新
driver.find_element(By.LINK_TEXT,'亲,请登录').click() #点击超链接‘亲,请登录’
print('请扫码!')
time.sleep(20) #休息20秒等待网页刷新
driver.get('https://cart.taobao.com/cart.htm') #进入购物车
time.sleep(5) #休息5秒等待网页刷新
while 1: #寻找全选按钮,找到后点击按钮然后退出循环
if driver.find_element(By.ID,'J_SelectAll1'):
driver.find_element(By.ID,'J_SelectAll1').click()
break
while 1: #寻找结算按钮,找到后点击结算然后退出循环
now=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') #系统现在时间
print(now)
if now>times: #判断是否到达抢购时间
try:
if driver.find_element(By.LINK_TEXT,'结 算'):
driver.find_element(By.LINK_TEXT, '结 算').click()
print('here')
break
except:
print('未找到结算按钮')
while 1: #寻找提交订单按钮,找到后点击提交订单然后退出循环
now=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(now)
if now>times:
try:
if driver.find_element(By.LINK_TEXT, '提交订单'):
driver.find_element(By.LINK_TEXT, '提交订单').click()
print('结算成功,我已帮你抢到商品了,请及时支付订单')
speaker.Speak('结算成功,我已帮你抢到商品了,请及时支付订单')
break
except:
pass
# time.sleep(2)
# driver.quit()
Achievements
j
conclusion
If you have any questions, you can leave a message in the comment area. Let's learn python together and make progress together! !