1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Python3+Selenium 实现自动登录淘宝+清空购物车

Python3+Selenium 实现自动登录淘宝+清空购物车

时间:2019-08-04 14:24:52

相关推荐

Python3+Selenium 实现自动登录淘宝+清空购物车

此博客的目的为分享自己用Python3和Selenium实现的自动登录淘宝和清空购物车的程序逻辑。经测试,此程序有时可以“秒杀”一些供给相对充足的限量商品,但无法秒杀疫情期间的任何定时限量口罩商品。

近期在家里看淘宝网上的定时限量口罩一直抢不到,就想办法写了个脚本,希望能实现自动化秒杀。可实现后发现仍旧无法秒杀口罩,仅能实现自动登录淘宝和清空购物车。索性我把它分享出来,希望它目前的功能会对你们有所帮助。

源代码

程序源码在我的GitHub上,想要的可以移步至/DoubleTrust/Taobao-Login-ClearProducts。如果对你有用的话可以点个star,谢谢了。

程序逻辑

此程序创建了一个AutoTaobao类,运行时的主要逻辑为:

登录淘宝;进入购物车;等待“秒杀”;提交订单,进入支付宝支付页面(密码自动输入,等待用户点击确认)。

十分简单的逻辑。其中,我在“登录”这一步写了3种登录方式:

# Manually login approach'''Warning: even for self-login, the system will pop up a verification bar after attempting to login in for many times'''# remainTime = 30# while remainTime >= 0:#prompt = "time remaining: " + str(remainTime) + "s for login."#print("\r" + prompt, end="", flush=True)#time.sleep(1)#remainTime -= 1

手动登录。这样做自然是最安全和方便程序猿的。当游览器打开时,若之前未设置“无图片”模式,用户可自行扫瞄二维码登录。不过因为我想做自动化登录,所以该方法最后被注视掉了。

''' Taobao login approach (sometimes it will trigger block identification, unable to resolve this problem) '''# wait.until(EC.presence_of_element_located((By.ID, 'TPL_username_1'))).send_keys('15320540266')# time.sleep(1)# wait.until(EC.presence_of_element_located((By.ID, 'TPL_password_1'))).send_keys('change.')# time.sleep(1)# wait.until(EC.presence_of_element_located((By.ID, 'J_SubmitStatic'))).click()

淘宝登陆。要自动化的话,显然最简单的就是在淘宝网上直接输入用户名密码,点击确认,然后就进入主页。但可惜的是,淘宝在不断更新自己的反爬虫机制,我让程序自动登录时总是会弹出“点击验证”这种验证方法,导致无法自动登录。因此该方法对我而言目前行不通。

''' Weibo login approach '''# if the time of typing username and password is too fast, it will trigger verification procedureself.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".weibo-login"))).click()time.sleep(3)self.wait.until(EC.presence_of_element_located((By.NAME, "username"))).send_keys(weibo_username)time.sleep(3)self.wait.until(EC.presence_of_element_located((By.NAME, "password"))).send_keys(weibo_password)time.sleep(1)self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "span[node-type='submitStates']"))).click()

微博登录。在淘宝网登录时可以选择用微博登录方式来替代,这就给了我机会。进入“微博登录”选项后,同样是输入账号和密码,点击登录。如果运气好可以直接跳转至淘宝网主页,运气不好的话就得触发验证码。关于验证码,我引用了“超级鹰”这个脚本帮我解决(详情可见源码),所以目前来看登录还是没有问题。至于为什么要sleep那么多次,那是为了防止用户名和密码输入太快而被反爬虫检测到,从而直接触发验证码验证机制。

熟悉Selenium的同学,应该会很容易理解我之后的代码了。不过关于ChromeDriver的设置,有几点我还想说一下:

# change the strategy of loading a page (no longer block the program until the page is completely loaded)self.desired_capabilities = DesiredCapabilities.CHROMEself.desired_capabilities["pageLoadStrategy"] = "none"# load Chrome driverself.options = webdriver.ChromeOptions()# for macself.chromedriver = r"/usr/local/bin/chromedriver"# for win#self.chromedriver = r"C:/Mac_files/University of Melbourne/PythonProjects/venuWin/Scripts/chromedriver"# use the core of chrome instead of opening an actual browser# self.options.headless = True# disguise to be a mobile to enhance speed (cannot scan QR code)# self.options.add_argument('user-agent={0}'.format('MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ2'#'2; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'))# stop loading picturesself.options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})# set the driverself.driver = webdriver.Chrome(self.chromedriver, options=self.options)# set to developer mode to avoid being recognized as crawling by some websites# old version# options.add_experimental_option('excludeSwitches', ['enable-automation'])# new versionscript = '''Object.defineProperty(navigator, 'webdriver', {get: () => undefined})'''self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})# implicitly waitsself.driver.implicitly_wait(30)# explicit waits (5 sec limitation, 0.1 frequency to access each time)self.wait = WebDriverWait(self.driver, 5, poll_frequency=0.1)

在程序初始化时,我便对ChromeDriver进行了一些设置,加快游览器的访问速度,以此达到秒杀级别(虽然最后并没能抢到口罩)。其中,我变更了页面加载模式,让代码在找到指定元素后,即便页面没加载完也能执行跳转;同时,我还设置了“无图片”模式和调整了Webdriver在游览器中的可见性(防止被监测并被视为爬虫);除此之外,我还减少了WebDriverWait每次检测的频率(0.1秒/次)。原本我想设置无头模式再进一步加快速度,但测试时发现无头模式下只能自动登录,无法自动清空购物车,原因暂时不明。

为什么不能秒杀

我思考了一下,觉得无法秒杀可能存在以下原因:

代码结构存在问题,未达到最优化;网速不够快;Selenium本身的限制;一些我目前没察觉出来的Bug。

如果大佬们觉得我的代码哪里有不足之处,欢迎提出来。我看到了会及时回复的,谢谢。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。