码迷,mamicode.com
首页 > 其他好文 > 详细

淘宝自动抢购

时间:2019-06-17 17:20:21      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:main   http   sleep   功能   car   color   nbsp   自动   use   

#!/usr/bin/env python
‘‘‘
    作者:张铭达
    功能:淘宝秒杀购物
    版本:0.1
    日期2019-06-16
‘‘‘

from  selenium import webdriver
import time,datetime

driver = webdriver.Chrome()
driver.maximize_window()

username = 张铭达33333
class TaoBao(object):
    def __init__(self):
        pass
    def login(self):
        driver.get(https://www.taobao.com)
        time.sleep(0.5)
        loginelement = driver.find_element_by_link_text("亲,请登录")
        if loginelement:
            loginelement.click()
        while True:
            try:
                login_status = driver.find_element_by_link_text(张铭达33333)
                print(已登录)
                return True
            except:
                print(等待扫码或密码登录...)
                time.sleep(1)

    def daojishi(self,buytime):
        ‘‘‘
        :param buytime: 输入字符串时间
        :return: 返回当前距离购买时间还有多少天,小时,分钟,秒
        ‘‘‘
        daojishi = datetime.datetime.strptime(buy_time, %Y-%m-%d %H:%M:%S) - datetime.datetime.now()
        ‘‘‘这个时间差为datetime.timedelta格式,返回值中days + seconds 为总计的时间,够一天的时间存在days里面,不足的存在seconds里面‘‘‘
        days = daojishi.days
        hour = daojishi.seconds // 3600 #不足一天,看有多少个小时。
        mins = (daojishi.seconds % 3600) // 60 #不足一小时的看有多少个分钟
        seconds = daojishi.seconds % 60 # 不足一分钟的时间看有多少秒
        return days,hour,mins,seconds #返回值便于更直观看剩余多少时间:时分秒
    def buy_all(self,buy_time):
        driver.get("https://cart.taobao.com/cart.htm")

        while True:
            ‘‘‘勾选购物车全部商品‘‘‘
            if driver.find_element_by_id("J_SelectAll1"):
                driver.find_element_by_id("J_SelectAll1").click()
            ‘‘‘ 对比当前时间到的话就点击结算,字符串可以直接比较时间大小‘‘‘
            now = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)
            if now > buy_time:
                try:
                    # 点击结算按钮
                    if driver.find_element_by_id("J_Go"):
                        driver.find_element_by_id("J_Go").click()
                        time.sleep(0.5)
                    #点击提交订单按钮
                    driver.find_element_by_link_text(提交订单).click()
                    print(订单已提交,请付款)
                except:
                    print(购物车没有商品或者已经提交。)
            else:
                days,hour,mins,seconds = self.daojishi(buy_time)
                print(‘当前{} 距离 {} 倒计时还有{}天 {}小时 {}分钟 {}秒.format(now,buy_time,days, hour, mins, seconds))
                if days > 1 or hour > 1 or mins > 2:
                    time.sleep(60)
                    driver.refresh()
            time.sleep(1)


if __name__ == __main__:
    taobao = TaoBao()
    if taobao.login():

        buy_time = 2019-06-17 23:59:59
        taobao.buy_all(buy_time)
        # taobao.daojishi(buy_time)

 

淘宝自动抢购

标签:main   http   sleep   功能   car   color   nbsp   自动   use   

原文地址:https://www.cnblogs.com/zhangmingda/p/11040527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!