【緊急情況】:回宿舍放下書包的我,花了20分鐘敲了一個搶購指令碼

專注的阿熊發表於2022-11-15

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(')

   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 = '

         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部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2923503/,如需轉載,請註明出處,否則將追究法律責任。

相關文章