ITPub博客

首页 > IT职业 > IT生活 > 【紧急情况】:回宿舍放下书包的我,花了20分钟敲了一个抢购脚本

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

IT生活 作者:专注的阿熊 时间:2022-11-15 17:19:56 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 =跟单网gendan5.com 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()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2923503/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276760