临近春节,脚本之家小编带领大家用Python抢火车票!
首先我们需要splinter
安装:
pip install splinter -i http://pypi.douban.com/simple –trusted-host pypi.douban.com
然后还需要一个浏览器的驱动,当然用chrome啦
下载地址:
http://chromedriver.storage.googleapis.com/index.html?path=2.20/
根据下载的自己的电脑系统选择下载包,我的windows就用win32了
解压后直接放到C:WindowsSystem32目录下,你当然也可以给这个驱动程序弄个环境变量。
注意:我下的驱动版本是2.19的,根据自己需要下载相应版本,我的2.20版本有报错
首先简单的测试一下吧,推荐ipython代替python自带的交互界面
from splinter.browser import Browser b = Browser(driver_name="chrome") b.visit("http://www.baidu.com") ###注意不要去掉http://
然后牛刀小试一下吧,用百度搜索一些东西。比如splinter
在上面我们已经打开百度的网址了
然后我们输入一些像搜索的内容吧
由上可以发现,该输入框的name=wd,通过fill似乎只能通过name填充
官方说明: Fill the field identified by name with the content specified by value.
那就在输入框搜索splinter,当然也可以输入中文,但是最好指定Unicode编码,如u”我”
b.fill("wd","splinter")
有意思的事,你会发现你都不需要点击“百度一下”就到搜索页面了
但是,如果多次搜索,我们还是需要点击“百度一下”的
下面就不在带着大家找这些元素的id,value什么的了,通过chrome的F12找自己需要的吧
那么把点击栏find出来吧
我们发现,百度搜索栏的value=”百度一下”,id=”su”
所以把这个按钮提取出来
button = b.find_by_value(u"百度一下")
或者
button = b.find_by_id(u"su")
怎么点击呢?简单如下
button.click()
这有什么用?
我们找找页面里有没有我想找的东西吧,比如找找有没有这个地址“splinter.cobrateam.info”
b.is_text_present("splinter.cobrateam.info")
如果该页面存在,则返回True,反之亦然
怎么退出呢?
b.quit()
好吧,上面就是参照官方文档写的一个简单的入门教程了,下面我们进入正题吧~~~
个人是觉得授人以鱼不如授人以渔的,所以我尽量讲解所有的内容,而非发个代码,让大家copy一下,然后不求甚解。
值得注意的是,我不会去说什么怎么破解验证码以及有什么漏洞可以利用什么的,抢过票的都知道,快一点是一点,而我要做的是就能将机器能做的交给机器做,比如点击,查询,选择等,所以希望必中的还是绕过此文吧。笔者水平也就这么一点点。
首先我们用ipython讲解一下思路
开始当然是导入啦。。
from splinter.browser import Browser b = Browser(driver_name="chrome") url = “https://kyfw.12306.cn/otn/leftTicket/init” b = Browser(driver_name="chrome") b.visit(url)
第一步手动登陆,能通过下面的代码填充表单,但是我跳不过验证码,暂时没有精力去研究那东西,多多见谅,所以还是等手动选择验证码的。
b.find_by_text(u"登录").click() b.fill("loginUserDTO.user_name","xxxx") b.fill("userDTO.password","xxxx")
第二部选择出发地点日期等
通过cookies选择出发地点,日期及目的地
首先瞧瞧我们的cookies当然是没有的出发日期什么的
至于你的出发地点及目的地对于的cookies值是什么,就得靠自己去copy了,我帮不了
怎么有的这些值?
先将地点日期输进去查询一下,然后chrome按F12 找到这一部分即可
打开浏览器跳到这个页面当然是没有我们需要的信息的,比如下面这样
b.cookies.all() {u'BIGipServerotn': u'1977155850.38945.0000', u'JSESSIONID': u'0A01D97598F459F751C4AE8518DBFB300DA7001B67', u'__NRF': u'95D48FC2E0E15920BFB61C7A330FF2AE', u'current_captcha_type': u'Z'}
然后我们需要添加出发地,这个得自己去查了,是简单的url加密
b.cookies.add({"_jc_save_fromStation":"%u4E0A%u6D77%2CSHH"})
添加出发日期
b.cookies.add({"_jc_save_fromDate":"2016-01-20"})
添加目的地
b.cookies.add({u'_jc_save_toStation':'%u6C38%u5DDE%2CAOQ'})
注:如果是修改的话,还是调用add方法,如果传入的字典key值已存在则替换
比如,将目的地改为其他地方xxxx,如下即可
b.cookies.add({u'_jc_save_toStation':'xxxxxx'})
以上内容给大家介绍了使用Python神器对付12306变态验证码,希望本文分享能够帮助到大家。

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor