注意事项:1.browser[WinError 2]错误:找不到文件
下载电脑对应版本的火狐驱动程序geckodriver.exe(我用的是火狐浏览器)
2. self.driver = Browser()默认浏览器为firefox
3.登录界面滑动移块报错:在self.driver.visit(self.login_url)后加上 :
script = ‘Object.defineProperty(navigator,“webdriver”,{get:()=>undefined,});’
self.driver.execute_script(script)
webdriver必须被设为undefied浏览器才不会被判断为处于自动化中。(网站才能认为是浏览器在访问)

代码是从github上下载的:

https://github.com/gxcuizy/Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-"""
通过splinter刷12306火车票
进入登陆页面,可以选择扫码登陆或者账号密码登陆
登陆成功后,接下来的事情,交由脚本来做了,静静的等待抢票结果就好(刷票过程中,浏览器不可关闭)
抢票成功,会进行手机短信和邮件的通知
author: cuizy
time: 2018-11-21
"""import re
from splinter.browser import Browser
from time import sleep
import sys
import httplib2
from urllib import parse
import smtplib
from email.mime.text import MIMEText
import timeclass BrushTicket(object):"""买票类及实现方法"""def __init__(self, passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email):"""定义实例属性,初始化"""# 乘客姓名self.passengers = passengers# 起始站和终点站self.from_station = from_stationself.to_station = to_station# 乘车日期self.from_time = from_time# 车次编号self.number = number.capitalize()# 座位类型所在td位置if seat_type == '商务座特等座':seat_type_index = 1seat_type_value = 9elif seat_type == '一等座':seat_type_index = 2seat_type_value = 'M'elif seat_type == '二等座':seat_type_index = 3seat_type_value = 0elif seat_type == '高级软卧':seat_type_index = 4seat_type_value = 6elif seat_type == '软卧':seat_type_index = 5seat_type_value = 4elif seat_type == '动卧':seat_type_index = 6seat_type_value = 'F'elif seat_type == '硬卧':seat_type_index = 7seat_type_value = 3elif seat_type == '软座':seat_type_index = 8seat_type_value = 2elif seat_type == '硬座':seat_type_index = 9seat_type_value = 1elif seat_type == '无座':seat_type_index = 10seat_type_value = 1elif seat_type == '其他':seat_type_index = 11seat_type_value = 1else:seat_type_index = 7seat_type_value = 3self.seat_type_index = seat_type_indexself.seat_type_value = seat_type_value# 通知信息self.receiver_mobile = receiver_mobileself.receiver_email = receiver_email# 新版12306官网主要页面网址self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'# 浏览器驱动信息,驱动下载页:https://sites.google.com/a/chromium.org/chromedriver/downloadsself.driver_name = 'chrome'self.driver = Browser(driver_name=self.driver_name)def do_login(self):"""登录功能实现,手动识别验证码进行登录"""self.driver.visit(self.login_url)sleep(1)# 选择登陆方式登陆print('请扫码登陆或者账号登陆……')while True:if self.driver.url != self.init_my_url:sleep(1)else:breakdef start_brush(self):"""买票功能实现"""# 浏览器窗口最大化self.driver.driver.maximize_window()# 登陆self.do_login()# 跳转到抢票页面self.driver.visit(self.ticket_url)try:print('开始刷票……')# 加载车票查询信息self.driver.cookies.add({"_jc_save_fromStation": self.from_station})self.driver.cookies.add({"_jc_save_toStation": self.to_station})self.driver.cookies.add({"_jc_save_fromDate": self.from_time})self.driver.reload()count = 0while self.driver.url == self.ticket_url:try:self.driver.find_by_text('查询').click()except Exception as error_info:print(error_info)sleep(1)continuesleep(0.5)count += 1local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print('第%d次点击查询……[%s]' % (count, local_date))try:current_tr = self.driver.find_by_xpath('//tr[@datatran="' + self.number + '"]/preceding-sibling::tr[1]')if current_tr:if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':print('无此座位类型出售,已结束当前刷票,请重新开启!')sys.exit(1)elif current_tr.find_by_tag('td')[self.seat_type_index].text == '无':print('无票,继续尝试……')sleep(1)else:# 有票,尝试预订print('刷到票了(余票数:' + str(current_tr.find_by_tag('td')[self.seat_type_index].text) + '),开始尝试预订……')current_tr.find_by_css('td.no-br>a')[0].click()sleep(1)key_value = 1for p in self.passengers:if '()' in p:p = p[:-1] + '学生' + p[-1:]# 选择用户print('开始选择用户……')self.driver.find_by_text(p).last.click()# 选择座位类型print('开始选择席别……')if self.seat_type_value != 0:self.driver.find_by_xpath("//select[@id='seatType_" + str(key_value) + "']/option[@value='" + str(self.seat_type_value) + "']").first.click()key_value += 1sleep(0.2)if p[-1] == ')':self.driver.find_by_id('dialog_xsertcj_ok').click()print('正在提交订单……')self.driver.find_by_id('submitOrder_id').click()sleep(2)# 查看放回结果是否正常submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].textif submit_false_info != '':print(submit_false_info)self.driver.find_by_id('qr_closeTranforDialog_id').click()sleep(0.2)self.driver.find_by_id('preStep_id').click()sleep(0.3)continueprint('正在确认订单……')self.driver.find_by_id('qr_submit_id').click()print('预订成功,请及时前往支付……')# 发送通知信息self.send_mail(self.receiver_email, '恭喜您,抢到票了,请及时前往12306支付订单!')self.send_sms(self.receiver_mobile, '您的验证码是:8888。请不要把验证码泄露给其他人。')else:print('不存在当前车次【%s】,已结束当前刷票,请重新开启!' % self.number)sys.exit(1)except Exception as error_info:print(error_info)# 跳转到抢票页面self.driver.visit(self.ticket_url)except Exception as error_info:print(error_info)def send_sms(self, mobile, sms_info):"""发送手机通知短信,用的是-互亿无线-的测试短信"""host = "106.ihuyi.com"sms_send_uri = "/webservice/sms.php?method=Submit"account = "C59782899"pass_word = "19d4d9c0796532c7328e8b82e2812655"params = parse.urlencode({'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json'})headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30)conn.request("POST", sms_send_uri, params, headers)response = conn.getresponse()response_str = response.read()conn.close()return response_strdef send_mail(self, receiver_address, content):"""发送邮件通知"""# 连接邮箱服务器信息host = 'smtp.163.com'port = 25sender = 'gxcuizy@163.com'  # 你的发件邮箱号码pwd = '******'  # 不是登陆密码,是客户端授权密码# 发件信息receiver = receiver_addressbody = '<h2>温馨提醒:</h2><p>' + content + '</p>'msg = MIMEText(body, 'html', _charset="utf-8")msg['subject'] = '抢票成功通知!'msg['from'] = sendermsg['to'] = receivers = smtplib.SMTP(host, port)# 开始登陆邮箱,并发送邮件s.login(sender, pwd)s.sendmail(sender, receiver, msg.as_string())if __name__ == '__main__':# 乘客姓名passengers_input = input('请输入乘车人姓名,多人用英文逗号“,”连接,(例如单人“张三”或者多人“张三,李四”,如果学生的话输入“王五()”):')passengers = passengers_input.split(",")while passengers_input == '' or len(passengers) > 4:print('乘车人最少1位,最多4位!')passengers_input = input('请重新输入乘车人姓名,多人用英文逗号“,”连接,(例如单人“张三”或者多人“张三,李四”):')passengers = passengers_input.split(",")# 乘车日期from_time = input('请输入乘车日期(例如“2018-08-08”):')date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')while from_time == '' or re.findall(date_pattern, from_time) == []:from_time = input('乘车日期不能为空或者时间格式不正确,请重新输入:')# 城市cookie字典city_list = {'bj': '%u5317%u4EAC%2CBJP',  # 北京'hd': '%u5929%u6D25%2CTJP',  # 邯郸'nn': '%u5357%u5B81%2CNNZ',  # 南宁'wh': '%u6B66%u6C49%2CWHN',  # 武汉'cs': '%u957F%u6C99%2CCSQ',  # 长沙'ty': '%u592A%u539F%2CTYV',  # 太原'yc': '%u8FD0%u57CE%2CYNV',  # 运城'gzn': '%u5E7F%u5DDE%u5357%2CIZQ',  # 广州南'wzn': '%u68A7%u5DDE%u5357%2CWBZ',  # 梧州南}# 出发站from_input = input('请输入出发站,只需要输入首字母就行(例如北京“bj”):')while from_input not in city_list.keys():from_input = input('出发站不能为空或不支持当前出发站(如有需要,请联系管理员!),请重新输入:')from_station = city_list[from_input]# 终点站to_input = input('请输入终点站,只需要输入首字母就行(例如北京“bj”):')while to_input not in city_list.keys():to_input = input('终点站不能为空或不支持当前终点站(如有需要,请联系管理员!),请重新输入:')to_station = city_list[to_input]# 车次编号number = input('请输入车次号(例如“G110”):')while number == '':number = input('车次号不能为空,请重新输入:')# 座位类型seat_type = input('请输入座位类型(例如“软卧”):')while seat_type == '':seat_type = input('座位类型不能为空,请重新输入:')# 抢票成功,通知该手机号码receiver_mobile = input('请预留一个手机号码,方便抢到票后进行通知(例如:18888888888):')mobile_pattern = re.compile(r'^1{1}\d{10}$')while receiver_mobile == '' or re.findall(mobile_pattern, receiver_mobile) == []:receiver_mobile = input('预留手机号码不能为空或者格式不正确,请重新输入:')receiver_email = input('请预留一个邮箱,方便抢到票后进行通知(例如:test@163.com):')while receiver_email == '':receiver_email = input('预留邮箱不能为空,请重新输入:')# 开始抢票ticket = BrushTicket(passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email)ticket.start_brush()

经研究短信发送部分使用不了,经过适当修改12306还是可以爬取的,只是爬学生票名字要写成“张三()"。
捣鼓了一个白天,虽然没有什么结果,但是挺有趣的。
每天一个小程序,加油!

1每天Python小例-12306爬虫#WinError 2相关推荐

  1. 3每天Python小例-爬取淘宝网页商品

    代码是从https://github.com/gxcuizy/Python/tree/master/%E4%BB%8E%E9%9B%B6%E5%AD%A6Python-%E6%8E%98%E9%87% ...

  2. python小代码_Python爬虫入门有意思的小长代码

    一段有意思的代码,有兴趣的可以研究研究. 需求 用户收到短信如:购买了电影票或者火车票机票之类的事件.然后app读取短信,解析短信,获取时间地点,然后后台自动建立一个备忘录,在事件开始前1小时提醒用户 ...

  3. Python 小项目 01 爬虫项目 爬取链家网南京地区二手房信息

    SpiderLianjia 介绍 python爬虫小程序,爬取链家网南京地区普通住宅二手房数据. 代码下载: https://gitee.com/lihaogn/SpiderLianjia 1 程序设 ...

  4. python频次统计图_科学网—Python小例:统计文本中单词出现的频次 - 康建的博文...

    import re zen=''' The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better ...

  5. 【python小课堂专栏】python小课堂31 - 初识原生爬虫

    python小课堂31 - 初识原生爬虫 前言 在上一期介绍了爬虫之前必会浏览器的开发者工具,忘记的童鞋可以在回顾一下: python小课堂30 - 爬虫之前必会的浏览器开发者工具. 本篇文章将以实战 ...

  6. 简单完整的Python小爬虫教程

    作者:vinyyu 声明:版权所有,转载请注明出处,谢谢. 1 调入需要使用的库 time库用于每次获取页面的时间间隔:pandas库用于DataFrame的数据格式存储:requests用于爬虫获取 ...

  7. python最简单的爬虫代码,python小实例一简单爬虫

    python新手求助 关于爬虫的简单例子 #coding=utf-8from bs4 import BeautifulSoupwith open('', 'r') as file: fcontent ...

  8. [Python]小百合十大爬虫

    国庆几天在家看了几篇关于使用Python来编写网络爬虫的博客,想来自己断断续续学习Python也有几个月了,但一个像样的程序都没有写过,编程能力并没有得到提高,愧对自己花费的时间.很多时候虽然知道什么 ...

  9. python小爬虫之天气查询

    python小爬虫之天气查询 刚开始研究爬虫,这个小程序通过抓取网页源代码,使用json解析实现了天气的查询. 1.需求分析 该博客实现了简单的天气查询功能,输入城市名称后可以查询出该城市的天气情况. ...

  10. Python小姿势 - # Python网络爬虫之如何通过selenium模拟浏览器登录微博

    Python网络爬虫之如何通过selenium模拟浏览器登录微博 微博登录接口很混乱,需要我们通过selenium来模拟浏览器登录. 首先我们需要安装selenium,通过pip安装: ``` pip ...

最新文章

  1. 医院选址问题--数据结构课程设计
  2. Ubuntu 14.04使用命令行安装VirtualBox
  3. 将声音转为图片(二维矩阵)
  4. html多重边框,中间空白,【基础】CSS实现多重边框的5种方式
  5. ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked
  6. mave工程中的一个类调用另一个聚合工程的一个类_谈谈设计模式:建造者模式在jdk中的体现,它和工厂模式区别?...
  7. powerpoint转换器_如何将PowerPoint演示文稿转换为主题演讲
  8. 计算机网络在我国的发展,04. 当前现状ISP典型架构 计算机网络在我国的发展
  9. python cx_Oracle连接Oracle数据库查询
  10. Djago模型层(基础)
  11. 【Spring】一次线上@Transational事务注解未生效的原因探究
  12. ADO.NET(二)
  13. 计算机恢复失败有杀毒,360安全卫士电脑清理结束不了,系统修复结束不了,木马查杀开始不了,电脑体检结束不了。怎么回事?...
  14. 《概率论与数理统计》重学笔记
  15. 重磅!中国三大数学奖全揭榜,8位数学大神获奖
  16. java libtorrent_[转载]libtorrent安装windows版
  17. Android 如何测试你的Base64是否正确展示
  18. maccms代码审计——前台sql注入漏洞
  19. PS与CSS字间距转换
  20. win7电脑如何提升开机速度?

热门文章

  1. 2012年8月26日
  2. MTK平台 后视镜系列 声控参数调整方法
  3. magento 修改订单状态为Completed
  4. 考研英语 - advanced
  5. 相对湿度和绝对湿度的区别
  6. [CTF]Brainfuck/Ook!编码
  7. 商业虚拟专用网络技术二通用路由封装
  8. Echarts使用感悟
  9. 安科瑞DTSD/ADL400通讯协议说明-Susie 周
  10. SpringMVC是什么?