splinter是Python中用于测试网站、模拟浏览网页的一个插件,操作简单
使用chrome前需要到官网下载chromedriver并解压缩复制到/usr/bin目录中
#####visit(‘http://’) 进入网页
#####fill() 函数可以在网页中输入内容
click() 模拟点击事件
find_by_id()
find_by_name()
find_by_text()
find_by_value() 查找网页中的元素
#####例如b.find_by_id(‘query_ticket’).click()即找到‘查询’并点击
#####还可以添加网页cookies,这能解决很多问题,如网页反爬虫的时候和添加一些历史输入信息(代码中的时间地点)
以下是我写的一直查询余票,如果能点进去预订后自动购票,并发送邮件通知用户支付。
刚大概写完,兴喜之余发出来,可能太多不足。当然解析网页还可以使用urllib、BeautifulSoup等库爬取。
#!/bin/bash
# -*- coding: utf8 -*-
from time import sleep
from splinter import exceptions
from splinter.browser import Browser
# 邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys
reload(sys)
sys.setdefaultencoding('utf8')
smtp_server = "smtp.163.com"
me = "user@163.com"
mail_to = "re@qq.com"
pwd = "password"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
# title
msg['Subject'] = u"抢票成功通知"
msg['From'] = me
msg['To'] = mail_to
pos = 1
name = u'购票人'
is_stu = u'(学生)'
user = '12306_user'
pwd = '12306_pwd'
confirm_url= 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
initmy_url = 'https://kyfw.12306.cn/otn/index/initMy12306'
ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
# 使用谷歌浏览器访问12306
b = Browser(driver_name = "chrome")
b.visit("https://kyfw.12306.cn/otn/")
# 进入登录界面
button = b.find_by_id('login_user')
button.click()
# 输入用户名及密码
b.fill('loginUserDTO.user_name', user)
b.fill('userDTO.password', pwd)
sleep(2)
while b.url != initmy_url:
sleep(1)
#==============================================================================
# b.find_by_id('loginSub').click()
#
# # 验证码是否正确
# if b.is_text_present(u'请点击下方验证码'):
# print 'login error'
# else:
# print 'login success'
#
#==============================================================================
def mail_to_user():
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\n抢票成功,请及时支付!!\nHere is the link you wanted:\nhttps://kyfw.12306.cn/otn/index/initMy12306"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
抢票成功,请及时支付!!<br>
Here is the <a href="https://kyfw.12306.cn/otn/index/initMy12306">link</a> you wanted.
</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP()
s.connect(smtp_server)
s.login(me, pwd)
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, mail_to, msg.as_string())
s.quit()
def check_ticket():
b.visit(ticket_url)
#==============================================================================
# b.find_by_id('fromStationText').fill(u'南京')
# b.find_by_id('toStationText').fill(u'重庆')
# 时间貌似无法直接填入,用cookies
# b.find_by_id('train_date').fill('2017-01-14')
#==============================================================================
# 加载地点、时间缓存(chrome >> application >> cookies)
b.cookies.add({'_jc_save_fromStation':'%u5357%u4EAC%2CNJH'})
b.cookies.add({'_jc_save_toStation':'%u91CD%u5E86%2CCQW'})
b.cookies.add({'_jc_save_fromDate':'2017-01-14'})
# 重载页面,查询
b.reload()
b.find_by_id('query_ticket').click()
sleep(1)
# click book ticket
try:
b.find_by_text(u'预订')[pos - 1].click()
except exceptions.ElementDoesNotExist:
print 'can not find book button error'
def book_ticket():
num = b.find_by_text(name + is_stu).__len__()
if num :
b.find_by_text(name + is_stu).click()
b.find_by_id('dialog_xsertcj_ok').click()
else:
b.find_by_text(name).click()
b.find_by_id('submitOrder_id').click()
sleep(1)
b.find_by_id('qr_submit_id').click()
if __name__ == '__main__':
while b.url != confirm_url:
check_ticket()
sleep(1)
# 查到票之后订票并邮件通知
book_ticket()
mail_to_user()
#b.quit()