用代码怎么在京东上抢1499元的茅台
时间: 2023-05-28 08:02:36 浏览: 126
首先,需要使用Python来实现自动化抢购。
1. 导入必要的库和模块
```python
import time
import selenium.webdriver as webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
```
2. 设置chrome浏览器并打开京东茅台的页面
```python
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\{username}\\AppData\\Local\\Google\\Chrome\\User Data') # 将{username}替换为你的计算机用户名
browser = webdriver.Chrome(chrome_options=options)
browser.get('https://item.jd.com/100012043978.html')
```
3. 等待页面加载并定位抢购按钮,点击抢购按钮
```python
while True:
try:
timer = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation')))
if timer.text == '立即预约':
timer.click()
elif timer.text == '已成功预约':
print('已成功预约')
break
except:
pass
```
4. 等待预约成功页面加载,并定位到抢购按钮,点击抢购按钮
```python
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation-attention')))
if btn.text == '提醒我':
btn.click()
elif btn.text == '已设置提醒':
print('已设置提醒')
break
except:
pass
```
5. 等待抢购页面加载,并定位到抢购按钮,点击抢购按钮
```python
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation-order')))
if btn.text == '立即抢购':
btn.click()
break
elif btn.text == '已添加到购物车':
print('已添加到购物车')
break
except:
pass
```
6. 等待购物车页面加载,并定位到提交订单按钮,点击提交订单按钮
```python
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.CLASS_NAME, 'submit-btn')))
if btn.text == '提交订单':
btn.click()
break
except:
pass
```
7. 等待订单确认页面加载,并勾选同意协议,提交订单
```python
while True:
try:
input = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'order-submit')))
if input.get_attribute('class') == 'submit-btn order-submit J_orderSubmit':
agree = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.CLASS_NAME, 'payment-bd-agree')))
if not agree.is_selected():
agree.click()
input.click()
break
except:
pass
```
完整代码如下:
```python
import time
import selenium.webdriver as webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\{username}\\AppData\\Local\\Google\\Chrome\\User Data') # 将{username}替换为你的计算机用户名
browser = webdriver.Chrome(chrome_options=options)
browser.get('https://item.jd.com/100012043978.html')
while True:
try:
timer = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation')))
if timer.text == '立即预约':
timer.click()
elif timer.text == '已成功预约':
print('已成功预约')
break
except:
pass
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation-attention')))
if btn.text == '提醒我':
btn.click()
elif btn.text == '已设置提醒':
print('已设置提醒')
break
except:
pass
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'btn-reservation-order')))
if btn.text == '立即抢购':
btn.click()
break
elif btn.text == '已添加到购物车':
print('已添加到购物车')
break
except:
pass
while True:
try:
btn = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.CLASS_NAME, 'submit-btn')))
if btn.text == '提交订单':
btn.click()
break
except:
pass
while True:
try:
input = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.ID, 'order-submit')))
if input.get_attribute('class') == 'submit-btn order-submit J_orderSubmit':
agree = WebDriverWait(browser, 1).until(EC.presence_of_element_located((By.CLASS_NAME, 'payment-bd-agree')))
if not agree.is_selected():
agree.click()
input.click()
break
except:
pass
```