程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes the rush purchase of a treasure (with super detailed code)

編輯:Python

Preface

618 In the past the , I did an earth shaking event the other day , It is estimated that this great event is what many little friends want to do . I used it python Rush to buy Taobao products , little does one think

Well , I am the bravest . On the idea of buying and code , I will introduce it in detail in this article , Those who are interested can look down !!!

Catalog

1. Project environment
2. Analysis of a treasure rush purchase process
3. Program implementation ideas
4. Code practice and sorting

Title Project Environment

• operating system :Windows 10
Python Exchange of learning Q Group :903971231####
• development environment :python3.7
•IDE:Pycharm
• Automation module :Selenium
• Installation command :pip install selenium
• Browser version :Google Chrome 99.0.4844.51
• Browser driven version :ChromeDriver 99.0.4844.51

Be careful : The browser driven version should be consistent with the browser version !

I have prepared for you 2022 Abreast of the times Python Free learning materials 、 Code 、 Tutorials, etc , Directly add a group to receive .

Analysis of rush purchase process

1. Login website

2. The shopping cart selects the items to be snapped up

3. Settle the purchase

4. place order

The key : Quickly complete the previous process , When the order enters the submission page , The payment time does not affect the rush purchase .

Program implementation ideas

The shopping process is fixed and unchanged , Therefore, you can edit the steps of the shopping process with code and submit them to selenium To automate .

Project structure : Drive direct copy into the project root path

1、 Import the corresponding class library to realize the operation of the browser

from selenium import webdriverimport datetime
import time
# Automatically open the browser and maximize the window driver = 
webdriver.Chrome()driver.maximize_window()

Execute the above code , The browser will automatically open and display as follows , It proves that we have started to manipulate the browser through code .

2、 Realize the shopping process with code

2.1 Visit a treasure :

driver.get('https://www.taobao.com')

2.2 Log in to a treasure :

if driver.find_element_by_partial_link_text(' Pro - , Please log in '):
driver.find_element_by_partial_link_text(' Pro - , Please log in ').click()

2.3 Into the shopping cart

# Jump to the shopping cart page driver.get('https://cart.taobao.com/cart.htm')

2.4 Select all items

# Look for all options and check if driver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()

2.5 Compare time , Submit settlement ( a key )

def buy(buy_time): #buy_time Time of purchase 
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(' current time :%s'%now)
# Judge whether the rush time is reached 
if now>buy_time:
try:
driver.find_element_by_partial_link_text(' junction count ').click()
except:
pass
# Compare time , Cycle order submission 
while True:
try:
if driver.find_element_by_link_text(' place order '):
driver.find_element_by_link_text(' place order ').click()
print(f" Panic buying , Please pay as soon as possible ")
except:
print(f" Try to submit the order again ")
time.sleep(0.01)

In this way, you can rush to buy goods at fixed time through the set period of time !

print(f" Try to submit the order again ")
time.sleep(0.01)
[ Outside the chain picture transfer in ...(img-oA7Q06eg-1648366756452)]
** In this way, you can rush to buy goods at fixed time through the set period of time !**

In this way, you can rush to buy goods at fixed time through the set period of time !

Last

About today's article , This is all my thinking , Give me a little heart if you like , Your support is my motivation , If you don't know anything about the article, you can send a private letter

Yo! !! See you in the next chapter !!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved