0


【紧急情况】:回宿舍放下书包的我,花了20分钟敲了一个抢购脚本

文章目录

情况紧急 ⁉️


不管你信不信,这就是俺刚瞧出的代码!!!
现在离20:00还有38分钟!!!
我现在在飞速的敲着文字,本来想着今晚简简单单买个东西就好,结果一看某宝,发现我想买的衣服有0元抢购的活动!!!

在这里插入图片描述
于是,刚吃完饭领完快递的我一到宿舍就开始劈里啪啦的敲起了代码,敲完代码后又进行一遍编的测试,终于将代码调试成功了。
具体实现如何,请往下看👇

抢❗️抢❗️抢❗️

本次代码实现的功能是抢某宝的商品,需要先将商品加入购物车,然后根据用户输入的开抢时间进行任务等待,时间一到,立即开抢!!!
注意:先设置好默认收货地址,然后将商品加入购物车!!!

开抢时间说明💨

在这里插入图片描述

开抢过程💥

Get_cookie.py

Get_cookie.py – 老朋友又来了,《小玩意儿》专栏中已经说烂咯,那在这就简单说明一下吧:
20秒内登录某宝,获取登录后的Cookie,保存为taobao_cookies.txt,这是实现自动登录前的必须一个步骤。

直接拿走💪

  1. from selenium import webdriver
  2. from time import sleep
  3. import json
  4. if __name__ =='__main__':
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get('https://login.taobao.com/member/login.jhtml?')sleep(20)
  8. dictCookies = driver.get_cookies() # 获取list的cookies
  9. jsonCookies = json.dumps(dictCookies) # 转换成字符串保存
  10. with open('taobao_cookies.txt','w') as f:
  11. f.write(jsonCookies)print('cookies保存成功!')

开抢

  1. from selenium import webdriver
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.common.by import By
  5. import datetime
  6. import time
  7. import json
  8. class Taobao(object):
  9. def __init__(self):
  10. # 登入以下网址即可直接跳转到购物车
  11. self.url = 'https://cart.taobao.com/cart.htm?'
  12. self.driver = webdriver.Chrome()
  13. self.driver.get(self.url)
  14. self.driver.maximize_window()
  15. # 自动登录
  16. def login(self):
  17. # 获取保存下的cookie值
  18. with open('taobao_cookies.txt','r', encoding='utf8') as f:
  19. listCookies = json.loads(f.read())
  20. # 往browser里添加cookies
  21. for cookie in listCookies:
  22. cookie_dict ={'domain':'.taobao.com','name': cookie.get('name'),'value': cookie.get('value'),"expires":'','path':'/','httpOnly': False,'HostOnly': False,'Secure': False
  23. }
  24. self.driver.add_cookie(cookie_dict)
  25. self.driver.refresh()
  26. # 等待快速登录按钮出现并点击
  27. WebDriverWait(self.driver,1000).until(
  28. EC.presence_of_element_located((By.XPATH,'//div[@class="fm-btn"]/button')))
  29. self.driver.find_element(By.XPATH,'//div[@class="fm-btn"]/button').click()
  30. def shopping_cart(self):
  31. # 设定时间等待,等到时间到了立即开抢
  32. startTime = datetime.datetime(2022,11,10,20,0,1)print('正在等待开抢...')while datetime.datetime.now()< startTime:
  33. time.sleep(1)
  34. # 点击全选
  35. WebDriverWait(self.driver,1000).until(
  36. EC.presence_of_element_located((By.XPATH,'//div[@id="J_SelectAll1"]')))
  37. self.driver.find_element(By.XPATH,'//div[@id="J_SelectAll1"]').click()
  38. # 等待结算按钮出现后点击
  39. WebDriverWait(self.driver,1000).until(
  40. EC.element_to_be_clickable((By.XPATH,'//a[@id="J_Go"]')))
  41. self.driver.find_element(By.XPATH,'//a[@id="J_Go"]').click()
  42. time.sleep(0.5)
  43. try:
  44. self.driver.find_element(By.XPATH,'//a[@id="J_Go"]').click()
  45. except:
  46. pass
  47. WebDriverWait(self.driver,1000).until(
  48. EC.element_to_be_clickable((By.LINK_TEXT,'提交订单')))
  49. # 等待“提交订单”元素加载完毕后点击
  50. self.driver.find_element(By.LINK_TEXT,'提交订单').click()
  51. def run(self):
  52. self.login()
  53. self.shopping_cart()
  54. taobao =Taobao()
  55. taobao.run()

结束语😱

如果喜欢,点赞👍 收藏🌈 关注哦💖
话不多说,开抢啦啦啦!!!
在这里插入图片描述

标签: python chrome 爬虫

本文转载自: https://blog.csdn.net/Oh_Python/article/details/127794789
版权归原作者 IT工藤新一 所有, 如有侵权,请联系我们删除。

“【紧急情况】:回宿舍放下书包的我,花了20分钟敲了一个抢购脚本”的评论:

还没有评论