Python之京東商品秒殺的實現(xiàn)示例
操作系統(tǒng):Windows 10Python版本:3.9.0Google Chrome 87.0.4280.88ChromeDriver 87.0.4280.88PyCharm 2020.2.3 x64
2 需求分析&前期準(zhǔn)備2.0 需求分析目標(biāo)是秒殺京東的訂單,這里面有幾個關(guān)鍵點,首先需要登錄京東,其次你需要準(zhǔn)備好訂單,最后要在指定時間快速提交訂單。登錄京東,這里就要用到一個爬蟲利器Selenium,它是一個自動化測試工具,利用它我們可以驅(qū)動瀏覽器執(zhí)行特定的動作,如點擊、下拉等等操作,所見即所得。另外對于一些 JavaScript 渲染的頁面來說,此種抓取方式非常有效。
2.1 Selenium的安裝Selenium 的安裝很簡單,dos命令行:
pip3 install selenium
Selenium安裝好之后,并不能直接使用,它需要與瀏覽器進(jìn)行對接。這里拿Chrome瀏覽器為例。若想使用Selenium成功調(diào)用Chrome瀏覽器完成相應(yīng)的操作,需要通過ChromeDriver來驅(qū)動。
2.2 ChromeDriver的安裝這里是ChromeDriver的官方下載地址。
鏈接:https://chromedriver.storage.googleapis.com/index.html下載之前先來確認(rèn)下我們使用的Chrome瀏覽器版本。
通過ChromeDriver的下載鏈接,找到與之對應(yīng)的Chrome瀏覽器版本,根據(jù)你電腦系統(tǒng)的平臺類型進(jìn)行下載。
下載完成之后,解壓,將其放置在Python安裝路徑下Scripts文件夾中即可
用PyCharm執(zhí)行如下代碼:
from selenium import webdriver# 打開Chrome瀏覽器driver = webdriver.Chrome()
成功打開瀏覽器,則證明ChromeDriver版本沒問題,即可正常使用Selenium。
from selenium import webdriverimport datetimeimport time# 打開Chrome瀏覽器driver = webdriver.Chrome() def auto_buy(username, password, purchase_list_time): print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '打開登陸界面') driver.get('https://passport.jd.com/new/login.aspx') print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '開始填寫賬號密碼') driver.find_element_by_link_text('賬戶登錄').click() driver.find_element_by_name('loginname').send_keys(username) driver.find_element_by_name('nloginpwd').send_keys(password) driver.find_element_by_id('loginsubmit').click() #print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '手動拼圖驗證') #time.sleep(10) #此處睡眠時間用來手動拼圖驗證 print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’),'登陸成功') print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '等待時間到達(dá)搶購時間:',purchase_list_time, '......') while True: count = 0 for buytime in purchase_list_time: nowtime = datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’) if nowtime == buytime:try: count += 1 print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '開始第 %s 次搶購......'%count) print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '打開購物車并選中商品') driver.get('https://cart.jd.com/cart_index') # 打開購物車并選中商品 # 如果沒有全選,點擊全選 if not driver.find_element_by_class_name(’jdcheckbox’).is_selected(): driver.find_element_by_class_name(’jdcheckbox’).click() print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '點擊去結(jié)算') driver.find_element_by_link_text('去結(jié)算').click() # 去結(jié)算 print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '點擊提交訂單') time.sleep(5) #提交訂單前必須等待幾秒【感覺跟電腦性能快慢有關(guān),不卡的電腦可以適當(dāng)降低嘗試】 if driver.find_element_by_id('order-submit'): driver.find_element_by_id('order-submit').click() # 提交訂單 print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’),'訂單提交成功,請前往訂單中心待付款付款') print('') continueexcept Exception as e: print(datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’), '搶購出現(xiàn)異常,重新?lián)屬? ', e) continue time.sleep(0.001) purchase_list_time = [ '2020-12-25 10:00:00', '2020-12-25 10:00:01', '2020-12-25 10:00:02', '2020-12-25 10:00:03', '2020-12-25 10:00:04', '2020-12-25 10:00:05',]auto_buy(’帳號’, ’密碼’, purchase_list_time)
預(yù)約商品到購物車——>修改代碼搶購時間——>用PyCharm運行代碼即可。
到此這篇關(guān)于Python之京東商品秒殺的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python 京東商品秒殺內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
