【紧急情况】:回宿舍放下书包的我,花了20分钟敲了一个抢购脚本

情况紧急 ⁉️


不管你信不信,这就是俺刚瞧出的代码!!!
现在离20:00还有38分钟!!!
我现在在飞速的敲着文字,本来想着今晚简简单单买个东西就好,结果一看某宝,发现我想买的衣服有0元抢购的活动!!!

在这里插入图片描述
于是,刚吃完饭领完快递的我一到宿舍就开始劈里啪啦的敲起了代码,敲完代码后又进行一遍编的测试,终于将代码调试成功了。
具体实现如何,请往下看👇

抢❗️抢❗️抢❗️

本次代码实现的功能是抢某宝的商品,需要先将商品加入购物车,然后根据用户输入的开抢时间进行任务等待,时间一到,立即开抢!!!
注意:先设置好默认收货地址,然后将商品加入购物车!!!

开抢时间说明💨

在这里插入图片描述

开抢过程💥

Get_cookie.py

Get_cookie.py – 老朋友又来了,《小玩意儿》专栏中已经说烂咯,那在这就简单说明一下吧:
20秒内登录某宝,获取登录后的Cookie,保存为taobao_cookies.txt,这是实现自动登录前的必须一个步骤。

直接拿走💪

from selenium import webdriver
from time import sleep
import json

if __name__ == '__main__':
  driver = webdriver.Chrome()
  driver.maximize_window()
  driver.get('https://login.taobao.com/member/login.jhtml?')
  sleep(20)

  dictCookies = driver.get_cookies() # 获取list的cookies
  jsonCookies = json.dumps(dictCookies) # 转换成字符串保存
  with open('taobao_cookies.txt', 'w') as f:
    f.write(jsonCookies)
  print('cookies保存成功!')

开抢

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import datetime
import time
import json


class Taobao(object):
    def __init__(self):
        # 登入以下网址即可直接跳转到购物车
        self.url = 'https://cart.taobao.com/cart.htm?'
        self.driver = webdriver.Chrome()
        self.driver.get(self.url)
        self.driver.maximize_window()
    # 自动登录
    def login(self):
        # 获取保存下的cookie值
        with open('taobao_cookies.txt', 'r', encoding='utf8') as f:
            listCookies = json.loads(f.read())

        # 往browser里添加cookies
        for cookie in listCookies:
            cookie_dict = {
                'domain': '.taobao.com',
                'name': cookie.get('name'),
                'value': cookie.get('value'),
                "expires": '',
                'path': '/',
                'httpOnly': False,
                'HostOnly': False,
                'Secure': False
            }
            self.driver.add_cookie(cookie_dict)

        self.driver.refresh()

        # 等待快速登录按钮出现并点击
        WebDriverWait(self.driver, 1000).until(
            EC.presence_of_element_located((By.XPATH, '//div[@class="fm-btn"]/button'))
        )
        self.driver.find_element(By.XPATH, '//div[@class="fm-btn"]/button').click()

    def shopping_cart(self):

            # 设定时间等待,等到时间到了立即开抢
        startTime = datetime.datetime(2022, 11, 10, 20, 0, 1)
        print('正在等待开抢...')
        while datetime.datetime.now() < startTime:
            time.sleep(1)

        # 点击全选
        WebDriverWait(self.driver, 1000).until(
            EC.presence_of_element_located((By.XPATH, '//div[@id="J_SelectAll1"]'))
        )
        self.driver.find_element(By.XPATH, '//div[@id="J_SelectAll1"]').click()

        # 等待结算按钮出现后点击
        WebDriverWait(self.driver, 1000).until(
            EC.element_to_be_clickable((By.XPATH, '//a[@id="J_Go"]'))
        )
        self.driver.find_element(By.XPATH, '//a[@id="J_Go"]').click()
        time.sleep(0.5)
        try:
            self.driver.find_element(By.XPATH, '//a[@id="J_Go"]').click()
        except:
            pass

        WebDriverWait(self.driver, 1000).until(
            EC.element_to_be_clickable((By.LINK_TEXT, '提交订单'))
        )
        # 等待“提交订单”元素加载完毕后点击
        self.driver.find_element(By.LINK_TEXT, '提交订单').click()

    def run(self):
        self.login()
        self.shopping_cart()


taobao = Taobao()
taobao.run()




结束语😱

如果喜欢,点赞👍 收藏🌈 关注哦💖
话不多说,开抢啦啦啦!!!
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>