引言环境准备代码飞猪签到领里程京东签到领京豆什么值得买签到 windows计划任务方法一方法二 总结参考
引言
Selenium是一个用于Web应用程序测试的工具。可以十分简单的模拟输入账户,密码,然后点击登陆按钮,在应对一些反爬虫机制时很有用。
本文利用python+Selenium尝试了几个网站(飞猪、京东、什么值得买)的自动签到,均测试可用。同时总结了Selenium实现这类任务的基本套路,操作十分简单,直接看代码即可。
环境:win7+python3.6
环境准备
1.安装Selenium
pip install selenium
2.下载chromedrive驱动
使用Selenium需要选择一个调用的浏览器并下载好对应的驱动,本文使用chrome浏览器,当然也可以用FireFox等,对应驱动放在python目录下面的scripts目录中,例如C:\Python36\Scripts
注:如果是macOS,下载对应版本驱动解压到环境变量包含的路径即可,比如/usr/local/bin
代码
1.飞猪签到领里程
#coding=utf-8from selenium import webdriverfrom mon.keys import Keysimport timemyusername = "your_user_name"#帐号mypassword = "your_password"#密码driver = webdriver.Chrome()driver.get("https://h5./trip/home/index.html?_projVer=0.1.116")content = driver.find_element_by_css_selector("li[data-trackname='Member']").click()if driver.title == "会员中心":time.sleep(10)driver.switch_to.frame(0)acount = driver.find_element_by_css_selector("input[name='TPL_username']")acount.clear()acount.send_keys(myusername)time.sleep(2)password = driver.find_element_by_css_selector("input[name='TPL_password']")password.clear()password.send_keys(mypassword)time.sleep(2)driver.find_element_by_css_selector("button[id='btn-submit']").click()while "圆圈" in driver.page_source:#有时需要一步验证,例如陌生IP登录driver.find_element_by_css_selector("span[class='km-dialog-btn']").click()time.sleep(2)password = driver.find_element_by_css_selector("input[name='TPL_password']")password.clear()password.send_keys(mypassword)time.sleep(2)driver.find_element_by_css_selector("div[class='click2slide-btn']").click()time.sleep(4)driver.find_element_by_css_selector("button[id='btn-submit']").click()time.sleep(5)if "立即签到" in driver.page_source:driver.find_element_by_css_selector("div[data-ref='76']").click()elif "已签到" in driver.page_source:driver.find_element_by_css_selector("div[data-ref='78']").click()
2.京东签到领京豆
#coding=utf-8from selenium import webdriverfrom mon.action_chains import ActionChains #导入鼠标操作from mon.keys import Keys #导入键值操作import timemyusername = "your_user_name"#帐号mypassword = "your_password"#密码signpage = "/home.html" #签到页browser = webdriver.Chrome()browser.get(signpage)try:jumplogin = browser.find_element_by_css_selector("#content > div.login-wrap > div.w > div > div.login-tab.login-tab-r > a")#切换到账户登录loginname = browser.find_element_by_id("loginname")password = browser.find_element_by_id("nloginpwd")submit = browser.find_element_by_id("loginsubmit")jumplogin.click()loginname.send_keys(myusername)password.send_keys(mypassword)submit.click()browser.implicitly_wait(5)if len(browser.find_elements_by_css_selector("body > div.floor-vip > div.w.clearfix > div.user-welfare > div.sign-in.signed > div.title")): #判断是否领取print("今日已领取!无需重复领取")else:signsubmit = browser.find_element_by_class_name("icon-sign") #签到规则signsubmit.click()print("领取成功!")except:print("京豆领取失败!")time.sleep(5)#退出驱动#browser.close()browser.quit()
3.什么值得买签到
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0import timeimport sysprint(sys.maxunicode)myusername = "your_user_name"#帐号mypassword = "your_password"#密码#开启浏览器驱动driver = webdriver.Chrome()driver.implicitly_wait(10)#打开网址driver.get("/")#窗口最大化#driver.maximize_window()#寻找登录按钮并且点击#driver.find_element_by_id('navBar_login').click()driver.find_element_by_css_selector("a[class='J_login_trigger']").click()#多层窗口定位driver.switch_to_frame('J_login_iframe')#输入用户名driver.find_element_by_id("username").send_keys(myusername)#driver.find_element_by_css_selector("input[name='username']").send_keys(myusername)#输入密码driver.find_element_by_id("password").send_keys(mypassword)#driver.find_element_by_css_selector("input[name='password']").send_keys(mypassword)#点击登录#driver.find_element_by_id('login_submit').click()driver.find_element_by_css_selector("input[id='login_submit']").click()#time.sleep(5)print('start wait')#WebDriverWait(driver,3).until(lambda driver : driver.find_element_by_id('navBar_login_Info').is_displayed()==True)WebDriverWait(driver,3).until(lambda driver : driver.find_element_by_css_selector("div[class='user-info J_info']").is_displayed()==True)print('end wait')#webElement = driver.find_element_by_id('user_info_tosign')webElement = driver.find_element_by_css_selector("a[class='J_punch']")if(webElement.text =='签到领积分'):webElement.click()else:print(webElement.text)time.sleep(5)#退出驱动driver.quit()
windows计划任务
脚本调试正常后可以添加进计划任务,每天自动运行。
计划任务程序->创建基本任务->配置
方法一:
注意:前提是py后缀的脚本文件默认打开方式是python
方法二:
这个配置意思是运行你的python.exe程序执行你的脚本(参数),与py文件默认打开方式无关
总结
1.找到最简洁、直接的登录页面,这样加载会比较快,多考虑移动端的页面
2.使用驱动打开浏览器driver = webdriver.Chrome()
,打开登录页面driver.get(url)
3.利用find_element_by_id
、find_element_by_css_selector
等API在页面中定位用户名、密码等元素,模拟点击登录
4.签到,判断签到成功
后续可移植到PhantomJS,部署到24小时运行的服务器上,利用crontab命令添加计划任务。
参考
python爬虫利器Selenium使用详解
Python爬虫利器五之Selenium的用法
搜狗测试-飞猪里程送你要不要?
利用python+selenium自动签到领京豆