Python implements simple Taobao flash sale function

不言
Release: 2018-05-03 15:35:16
Original
5568 people have browsed it

This article mainly introduces the simple Taobao flash sale function in python in detail. It has certain reference value. Interested friends can refer to it.

The example of this article shares with you the Python Taobao flash sale function. The specific code is for your reference. The specific content is as follows

Yesterday, Moutai launched a flash sale online. I originally thought that I could use my amazing hand speed to buy a bottle in seconds, but the result was. . . .

So I learned from the experience and thought it would be more reliable to use a script. I searched and searched on the Internet, and when I saw selenium, my eyes suddenly lit up!

The overall process is: Go to Taobao->Find the login button to log in->Select a password to log in on the login page->Fill in the account password to log in->Jump to the shopping cart page->Click All Select the button, wait for the agreed time, and settle and place the order immediately when the time is up.

Code:

import os
from selenium import webdriver
import datetime
import time

#需要下载deckodreiver
firefox = os.path.abspath(r"E:\Program Files\Mozilla Firefox\firefox.exe")
os.environ["webdriver.firefox.bin"] = firefox
driver = webdriver.Firefox()
driver.maximize_window()

def login(uname, pwd):
 driver.get("https://www.taobao.com")
 if driver.find_element_by_link_text("亲,请登录"):
  driver.find_element_by_link_text("亲,请登录").click()
 input(uname, pwd)

 #点击购物车里全选按钮
 if driver.find_element_by_id("J_SelectAll1"):
  driver.find_element_by_id("J_SelectAll1").click()
 # time.sleep(3)
 now = datetime.datetime.now()
 print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))

def input(uname,pwd):
 time.sleep(3)
 #选择密码登录
 if driver.find_element_by_id("J_Quick2Static"):
  driver.find_element_by_id("J_Quick2Static").click()
 time.sleep(3)

 #用户名输入
 if driver.find_element_by_name("TPL_username"):
  for i in uname:
   driver.find_element_by_name("TPL_username").send_keys(i)
   time.sleep(0.5)
 time.sleep(3)

 #密码输入
 if driver.find_element_by_name("TPL_password"):
  for j in pwd:
   driver.find_element_by_name("TPL_password").send_keys(j)
   time.sleep(0.5)
 time.sleep(3)

 #点击登录按钮
 if driver.find_element_by_id("J_SubmitStatic"):
  driver.find_element_by_id("J_SubmitStatic").click()
 time.sleep(3)
 driver.get("https://cart.taobao.com/cart.htm")
 time.sleep(2)

def buy(buytime):
 while True:
  now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  if now == buytime:
    try:
     #点击结算按钮
     if driver.find_element_by_id("J_Go"):
      driver.find_element_by_id("J_Go").click()
     driver.find_element_by_link_text('提交订单').click()
    except:
     time.sleep(1)
  print(now)
  time.sleep(1)
if __name__=="__main__":
 #中文账号记得decode编码
 login("账号",'密码')
 buy('2018-01-30 13:35:00')
Copy after login

A lot of threads are used to sleep here because the input is too fast and it will appear when logging in. Slide the verification box, so sleep for a few seconds and try to imitate human operations.

Related recommendations:

Example of Python implementation of exporting data to excel

Example of Python implementation to confirm whether a string contains the specified string

The above is the detailed content of Python implements simple Taobao flash sale function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template