跟着大大学python(54)

Python自动上京东抢手机~双十一买买买 过了还有双十二

# _*_coding:utf-8_*_
from selenium import webdriver
import datetime
import time


driver = webdriver.Firefox()

def login(uname, pwd):
    driver.get("http://www.jd.com")
    driver.find_element_by_link_text("你好,请登录").click()
    time.sleep(3)
    driver.find_element_by_link_text("账户登录").click()
    driver.find_element_by_name("loginname").send_keys(uname)
    driver.find_element_by_name("nloginpwd").send_keys(pwd)
    driver.find_element_by_id("loginsubmit").click()
    time.sleep(3)
    driver.get("https://cart.jd.com/cart.action")
    time.sleep(3)
    driver.find_element_by_link_text("去结算").click()
    now = datetime.datetime.now()
    print now.strftime('%Y-%m-%d %H:%M:%S')
    print 'login success'



def buy_on_time(buytime):
    while True:
        now = datetime.datetime.now()
        if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:
            while True:
                try:
                    driver.find_element_by_id('order-submit').click()
                except:
                    time.sleep(0.1)
        time.sleep(0.5)
        print now.strftime('%Y-%m-%d %H:%M:%S')
        print 'purchase success'


# entrance
login('XXXX', 'XXXX')
buytime = '2017-11-19 19:51:30'
buy_on_time(buytime)

感谢大大提供原代码和思路,在原基础上稍加修改,目前最大的问题是先加入购物车!

你可能感兴趣的:(跟着大大学python(54))