• 对淘宝秒杀程序的实践【2022.9】


    编程与库的环境

    Python版本:3.7(具体是几忘了,懒得查了)
    selenium库 版本:4.4.3

    成果与结果

    程序能跑,但是抢热门商品成功率0% QAQ

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import datetime
    import time
    from os import path
    
    d = path.dirname(__file__)
    abspath = path.abspath(d)
    
    # 连接Chrome浏览器
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path="C:/Program Files/Google/Chrome/Application/chromedriver", options=options)
    
    driver.maximize_window()#最大化浏览器
    
    
    def login():
      # 打开淘宝登录页,并进行扫码登录
      driver.get("https://www.taobao.com")
      time.sleep(3)
      if driver.find_element(By.LINK_TEXT,"亲,请登录"):
        driver.find_element(By.LINK_TEXT,"亲,请登录").click()
    
      print("请在20秒内完成扫码")
      time.sleep(20)
    
      driver.get("https://cart.taobao.com/cart.htm")
      time.sleep(5)
      # 点击全选按钮
      while 1 == 1:
          if driver.find_element(By.ID,"J_SelectAll1"):
              driver.find_element(By.ID,"J_SelectAll1").click()
              break
      print("login success")
    
    
    
    def buy(buytime):
      while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        print('Now:', now)
      # 对比时间,时间到的话就点击结算
        if now > buytime:
         try:
            # 点击结算按钮
            while 1==1:
                if driver.find_element(By.ID,"J_Go"):
                    driver.find_element(By.ID,"J_Go").click()
                    break
            if driver.find_element(By.LINK_TEXT,"提交订单"):
                driver.find_element(By.LINK_TEXT,"提交订单").click()
         except:
          time.sleep(0.1)
          print("fail:",now)
          time.sleep(0.1)
    
    
    if __name__ == "__main__":
      times = "2022-09-11 10:00
      
      :00.000000" # 时间格式:"2018-09-06 11:20:00.000000"
      login()
      buy(times)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    主要实践步骤的参考

    关于代吗的视频讲解
    代码参考

    实践过程中遇到的问题以及解决的办法

    报错’chromedriver’ executable needs to be in PATH

    解决办法:下载Chrome浏览器的webdriver
    记得webdriver的版本选择与chrome版本最相近的即可
    在运用selenium库后,我利用绝对路径进行打开,避免了网上说要把这安装包放在python根目录下和Chrome根目录下这一麻烦
    相关代碎片:

    from selenium import webdriver
    
    # 连接Chrome浏览器
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path="C:/Program Files/Google/Chrome/Application/chromedriver", options=options)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    报错‘WebDriver’ object has no attribute 'find_element_by_link_text’

    参考大部分博文,得知find_element_by_属性新版本的selenium库中已被弃用
    大部分都说改成driver.find_element(By.属性,"文字")后即可
    但是在实际的应用过程中,还是会报错,经过参考,发现还需要导入By才可以使用
    相关代码碎片:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    if driver.find_element(By.LINK_TEXT,"亲,请登录"):
        driver.find_element(By.LINK_TEXT,"亲,请登录").click()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    报错‘WebDriver’ object has no attribute 'find_element_by_id’

    同上,利用了相同的解决办法
    相关代码碎片:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    while 1==1:
    	if driver.find_element(By.ID,"J_Go"):
        	driver.find_element(By.ID,"J_Go").click()
            break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    不知道去掉死循环后能不能行,还没试过。。。毕竟只要程序能动、而且效率还不错的情况下,就不要轻易地去动它~

    反思

    科技确实改变命运,比如倒爷可以在互联网上出售货物啦QAQ东西更难抢了,可恶!!!
    但往非功利的角度来看(对对对我在试图安慰我自己QWQ),这一次的实践中浅尝了一下爬虫,也学会了怎么F12以后审查元素,要想用鼠标指哪就能定位到哪里,需要点一个按钮。
    在这里插入图片描述
    然后发现除了在购物车里秒杀之外,还有一种……需要到点反复刷新失效的宝贝界面,等客服人工上货以后才能买到的那种秒杀。有点想试,但最近没钱了【落泪】。
    有空的话会试试滴,但我对它的成功率不抱什么希望【叹气】。

  • 相关阅读:
    [附源码]计算机毕业设计JAVA面试刷题系统
    学习Android的第二十七天
    Oracle使用遇到的问题
    大模型时代的科研人员的思考
    仿SpringBoot启动Dome实现
    日志集中审计系列(5)--- LogAuditor接收USG设备日志
    非科班菜鸡算法学习记录 | 代码随想录算法训练营第57天|| 647. 回文子串 516.最长回文子序列 动态规划总结篇
    【PyTorch】TensorBoard基本使用
    【zookeeper】zookeeper集群安装
    HACKTHEBOX——Mirai
  • 原文地址:https://blog.csdn.net/weixin_45945586/article/details/126807636