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

RS 技术•随笔来源:CSDN评论790字数 2515阅读8分23秒阅读模式

编程与库的环境

Python版本:3.11

selenium库 版本:4.6

成果与结果

程序能跑,但是抢热门商品成功率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)

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

报错’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)

报错‘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()

报错‘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

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

反思

科技确实改变命运,比如倒爷可以在互联网上出售货物啦QAQ东西更难抢了,可恶!!!

但往非功利的角度来看(对对对我在试图安慰我自己QWQ),这一次的实践中浅尝了一下爬虫,也学会了怎么F12以后审查元素,要想用鼠标指哪就能定位到哪里,需要点一个按钮。

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

然后发现除了在购物车里秒杀之外,还有一种……需要到点反复刷新失效的宝贝界面,等客服人工上货以后才能买到的那种秒杀。有点想试,但最近没钱了。

有空的话会试试滴,但我对它的成功率不抱什么希望。

 

继续阅读
 
RS
  • 版权声明 本文源自 CSDNRS 整理 发表于 2022年11月21日 19:49:32
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定