如何通過python+Chrome實(shí)現(xiàn)自動(dòng)申請(qǐng)某東試用
在這里小小推薦下我的個(gè)人博客
csdn:雷園的csdn博客
個(gè)人博客:雷園的個(gè)人博客
簡(jiǎn)書:雷園的簡(jiǎn)書
注意*
本人python小白剛剛?cè)腴T,大神們多多指導(dǎo),這個(gè)小腳本也是有個(gè)小問題就是在剛啟動(dòng)后進(jìn)行登陸時(shí)需要手動(dòng)點(diǎn)擊驗(yàn)證,預(yù)留了八秒的時(shí)間進(jìn)行驗(yàn)證,展示手速的時(shí)候到啦。
環(huán)境
1.首先你需要有python,ide強(qiáng)烈推薦使用pycharm,因?yàn)榉浅:糜谩?/p>
2.你需要安裝seleium庫。教程連接
3.你需要下載chromedriver。附上下載鏈接
4.當(dāng)然,如果你使用pycharm的話跳轉(zhuǎn)我的另一篇博客:pycharm環(huán)境配置
代碼實(shí)現(xiàn)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome()
driver.get("https://try.m.jd.com/activities?cid1=0&state=0&ran=1540783336157")
#間隔三秒后進(jìn)入我的試用進(jìn)行登錄
time.sleep(3)
login=driver.find_element_by_xpath("/html/body/ul/li[3]/a")
ActionChains(driver).click(login).perform()
time.sleep(2)
username=driver.find_element_by_id("username")
password=driver.find_element_by_id("password")
username.send_keys("your_username")
password.send_keys("your_password")
time.sleep(1)
btn=driver.find_element_by_id("loginBtn")
ActionChains(driver).click(btn).perform()
time.sleep(8)
start=driver.find_element_by_xpath("/html/body/ul/li[2]/a")
ActionChains(driver).click(start).perform()
#此處應(yīng)該有循環(huán)
for i in range(12,10000):
time.sleep(2)
str1 = str(i)
#點(diǎn)擊商品
goods=driver.find_element_by_xpath("/html/body/div[3]/div[3]/div[1]/ul/li["+str1+"]")
if(goods is not None):
ActionChains(driver).click(goods).perform()
time.sleep(2)
#獲取商品申請(qǐng)按鈕的名稱
shen=driver.find_element_by_xpath("/html/body/ul/li[2]/a")
ActionChains(driver).click(shen).perform()
time.sleep(1)
#純靠運(yùn)氣
q=driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[2]/div[2]/a[2]")
if(q is not None):
ActionChains(driver).click(q).perform()
time.sleep(1)
#后退
driver.back()
if(i%12==0):
js="var q=document.documentElement.scrollTop=100000"
driver.execute_script(js)
time.sleep(3)
最后
最后嘛~右鍵運(yùn)行就可以啦!如果你是windows。配置好環(huán)境變量之后win+r輸入cmd進(jìn)入dos輸入正確的.py文件的路徑運(yùn)行腳本就可以啦。記得把chromedriver.exe和.py文件放到同一目錄下。
如果使用pycharm的話如下圖??點(diǎn)擊run '你的文件名'就可以啦

pycharm運(yùn)行方法