Blogger Information
Blog 75
fans 0
comment 0
visits 54663
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
小猿圈Python之实现京东秒杀功能技巧
聆听的博客
Original
948 people have browsed it

现在电商盛起,活动页面一个接着一个,一般都是抢购、秒杀,看着很刺激吧,这篇文章小猿圈加加就揭开这些秒杀功能的面纱,带你们看看实际的面貌,让我们学习一下代码吧。

实例

#Python3.5

#coding:utf-8

#import scrapy

from selenium import webdriver

import time

import datetime

driver = webdriver.Firefox()

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

class JDLOGIN(object):

  def login_jd(self,num,pwd):

    driver.find_element_by_link_text('你好,请登录').click()

    time.sleep(10)

    driver.find_element_by_link_text('账户登录').click()

    time.sleep(3)

    driver.find_element_by_id('loginname').send_keys(num)

    driver.find_element_by_id('nloginpwd').send_keys(pwd)

    time.sleep(3)

    driver.find_element_by_id('loginsubmit').click()

    time.sleep(5)

    nowwhandle = driver.current_window_handle

    driver.find_element_by_link_text('我的购物车').click()

    allhandles = driver.window_handles

    for handle in allhandles:

      if handle != nowwhandle:

        driver.switch_to_window(handle)#切换至窗口 购物车页面

    time.sleep(5)

    driver.find_element_by_link_text('去结算').click()

    time.sleep(4)

  def buy_on_time(self,buytime):

    while True:

      now = datetime.datetime.now()

      if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:

        driver.find_element_by_id('order-submit').click()

        time.sleep(3)

        print(now.strftime('%Y-%m-%d %H:%M:%S'))

        print('successful!!!')

      time.sleep(0.5)

      # 返回原先到的窗口

      #driver.switch_to_window(nowwhandle)

  def start(self,num,pwd,buytime):

    self.login_jd(num,pwd)

    self.buy_on_time(buytime)

jdlogin=JDLOGIN()

jdlogin.start('手机号','密码','秒杀时间' )

运行实例 »

点击 "运行实例" 按钮查看在线实例

需要先将秒杀物品添加至购物车(必须为秒杀物品,秒杀无品加入购物车后结算时不需要勾选【全选框】),并提前设置默认收货地址。该代码为闲时所写,没有进一步优化,面向对象的函数也不够精简,不过功能已经实现需要在cmd命令行安装selenium包:pipinstallselenium。

上面就是实现京东秒杀功能代码了,现在那些神秘感变得没有那么强烈了吧,代码就这么多,感兴趣的记得在自己的环境试试,做一个简单的页面,看能不能实现秒杀功能,***,小猿圈加加给你们满满的干货,喜欢的话点个赞吧。

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!