今天扣丁学堂Python培训课程主要给大家分享关于Python实现秒杀功能的具体代码,下面我们一起来看一下吧。
整体流程是:登录网址->找到登录按钮去登陆->在登录页选择密码登录->填入账号密码登录->跳到购物车页->点击全选按钮,等约定时间,时间到马上结算、下单。
代码:
importos
fromseleniumimportwebdriver
importdatetime
importtime
#需要下载deckodreiver
firefox=os.path.abspath(r"E:\ProgramFiles\MozillaFirefox\firefox.exe")
os.environ["webdriver.firefox.bin"]=firefox
driver=webdriver.Firefox()
driver.maximize_window()
deflogin(uname,pwd):
driver.get("https://www.taobao.com")
ifdriver.find_element_by_link_text("亲,请登录"):
driver.find_element_by_link_text("亲,请登录").click()
input(uname,pwd)
#点击购物车里全选按钮
ifdriver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()
#time.sleep(3)
now=datetime.datetime.now()
print('loginsuccess:',now.strftime('%Y-%m-%d%H:%M:%S'))
definput(uname,pwd):
time.sleep(3)
#选择密码登录
ifdriver.find_element_by_id("J_Quick2Static"):
driver.find_element_by_id("J_Quick2Static").click()
time.sleep(3)
#用户名输入
ifdriver.find_element_by_name("TPL_username"):
foriinuname:
driver.find_element_by_name("TPL_username").send_keys(i)
time.sleep(0.5)
time.sleep(3)
#密码输入
ifdriver.find_element_by_name("TPL_password"):
forjinpwd:
driver.find_element_by_name("TPL_password").send_keys(j)
time.sleep(0.5)
time.sleep(3)
#点击登录按钮
ifdriver.find_element_by_id("J_SubmitStatic"):
driver.find_element_by_id("J_SubmitStatic").click()
time.sleep(3)
driver.get("https://cart.taobao.com/cart.htm")
time.sleep(2)
defbuy(buytime):
whileTrue:
now=datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')
ifnow==buytime:
try:
#点击结算按钮
ifdriver.find_element_by_id("J_Go"):
driver.find_element_by_id("J_Go").click()
driver.find_element_by_link_text('提交订单').click()
except:
time.sleep(1)
print(now)
time.sleep(1)
if__name__=="__main__":
#中文账号记得decode编码
login("账号",'密码')
buy('2018-01-3013:35:00')
以上就是关于python实现简单秒杀功能的详细技术,提醒这里用了很多线程休眠,是因为太快的输入,在登录的时候回出现滑动验证框,所以sleep几秒,尽量模仿人的操作。希望对大家的学习有所帮助,也希望大家多多支持扣丁学堂。
网友评论