淘宝秒杀半价前N名半价商品

首先需安装lxml与selenium两个模块(使用selenium模块前需要下载一个Chromedriver插件,这个网上搜索下教程就好了,这里就不详细说啦)


```python
###Author:小白
##time:2019/11/9
##function:抢淘宝半价的商品(购物车不能出现可以0点前付款的商品,不然会直接付款.这里就不多说啦,直接上代码)
from time import sleep
from lxml import etree
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TaoBao(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.header_url = 'https://www.taobao.com/?spm=a2107.1.1000340.1.2b0011d9ziQXgO'  ##淘宝首页
        self.login = 'https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Fcart.taobao.com%2Fcart.htm%3Fspm%3Da21bo.2017.1997525049.1.5af911d9M32V6G%26from%3Dmini%26pm_id%3D1501036000a02c5c3739'  ##登入界面
        self.end_url = 'https://cart.taobao.com/cart.htm?spm=a21bo.2017.1997525049.1.472311d96Bs7iB&from=mini&pm_id=1501036000a02c5c3739'    ##购物车界面
        self.out_money = 'https://buy.tmall.com/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined'
        self.zhifu = 'https://cashiereu126.alipay.com/standard/lightpay/lightPayCashier.htm?orderId=110993de4deb31098549014584577193&bizIdentity=trade10001&outBizNo=2019110922001177195724641795&timeStamp=1573296316338&country=CN'
    def _logn(self):
        self.driver.get(self.header_url)                 ##打开首页
        wait = WebDriverWait(self.driver,1000)
        wait.until(EC.presence_of_element_located((By.ID,'mc-menu-hd')))   ##等待首页中购物车字出现
        self.driver.find_element_by_id('mc-menu-hd').click()               ##点击购物车
        # wait.until(EC.presence_of_element_located((By.ID,'J_Quick2Static')))   ##登入界面扫描修改为输入账号及密码
        # self.driver.find_element_by_id('J_Quick2Static').click()                ##点击                             ##你们可以扫描登入,或者输入账号密码,但是要等待几秒钟要输入账号密码,不然会出现滑动的验证,我这里是扫描登入。
        # wait.until(EC.presence_of_element_located((By.ID, 'TPL_username_1')))   ##等待输入框出现
        # self.driver.find_element_by_id('TPL_username_1').send_keys('艾美佳人壹号')
        # wait.until(EC.url_to_be(self.end_url))
        print('登入成功')
        print('正在开抢.....')
        sleep(2)
    def buy_url(self):
        self.driver.get(self.end_url)  ##进入购物车界面
        wait = WebDriverWait(self.driver, 1000)
        # div = html.xpath('/html/body/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div/@class')
        # print(div)
        # print(type(div))
        while True:    ###验证是否可以出现可以付款的商品,没有就一直刷新,有就选中付款。

            wait.until(EC.element_to_be_clickable((By.ID, 'J_CartMain')))
            url = self.driver.page_source
            html = etree.HTML(url)
            div = html.xpath('/html/body/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div/@class')
            for a in div:
                if 'select-all-disabled' in a :
                    print('刷新中....')
                    self.driver.refresh()
                    sleep(1)
                else:
                    self.driver.find_element_by_id("J_SelectAll1").click()
                    self.driver.find_element_by_class_name("submit-btn").click()
                    wait.until(EC.url_to_be(self.out_money))
                    break

    def hand_in(self):
        self.driver.get(self.out_money)
        wait = WebDriverWait(self.driver, 1000)
        wait.until(EC.presence_of_element_located((By.CLASS_NAME,'go-btn')))
        self.driver.find_element_by_link_text('提交订单').click()
        wait.until(EC.url_to_be(self.zhifu))
        self.driver.get(self.zhifu)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[1]').send_keys(*)  ##输入支付密码
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[2]').send_keys(*)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[3]').send_keys(*)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[4]').send_keys(*)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[5]').send_keys(*)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/span[2]/div/i[6]').send_keys(*)
        self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[3]/div/input').click()


    def run(self):
        self._logn()
        self.buy_url()
        self.hand_in()
        sleep(10)
if __name__ == '__main__':
    taobao = TaoBao()
    taobao.run()



你可能感兴趣的:(python,配置,双十一抢购前N名商品,双十一抢半价商品)