Python+Splinter实现简单12306自动刷新抢票

实现该功能(window)环境:

        1.python环境的搭建和安装;(测试版本是python 3.+)

        2.Splinter web测试工具;

第一:python,下载地址:https://www.python.org/ 官网,可以下载3.+版本即可,系统环境配置;(百度搜一下即可)

第二:Splinter 下载安装

           下载地址:https://pypi.python.org/pypi/splinter/ 最新版本0.7.7,window下载下来是tar.gz,直接用window 解压软件即可。

            1.用window cmd 指定到解压splinter的位置

               

            2.输入指令python setup.py build;

D:\splinter-0.7.7\splinter-0.7.7>python setup.py build
running build
running build_py
creating build
creating build\lib
creating build\lib\splinter
copying splinter\browser.py -> build\lib\splinter
copying splinter\cookie_manager.py -> build\lib\splinter
copying splinter\element_list.py -> build\lib\splinter
copying splinter\exceptions.py -> build\lib\splinter
copying splinter\meta.py -> build\lib\splinter
copying splinter\utils.py -> build\lib\splinter
copying splinter\__init__.py -> build\lib\splinter
creating build\lib\splinter\driver
copying splinter\driver\djangoclient.py -> build\lib\splinter\driver
copying splinter\driver\element_present.py -> build\lib\splinter\driver
copying splinter\driver\flaskclient.py -> build\lib\splinter\driver
copying splinter\driver\lxmldriver.py -> build\lib\splinter\driver
copying splinter\driver\zopetestbrowser.py -> build\lib\splinter\driver
copying splinter\driver\__init__.py -> build\lib\splinter\driver
creating build\lib\splinter\request_handler
copying splinter\request_handler\status_code.py -> build\lib\splinter\request_handler
copying splinter\request_handler\__init__.py -> build\lib\splinter\request_handler
creating build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\chrome.py -> build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\cookie_manager.py -> build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\firefox.py -> build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\phantomjs.py -> build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\remote.py -> build\lib\splinter\driver\webdriver
copying splinter\driver\webdriver\__init__.py -> build\lib\splinter\driver\webdriver
running egg_info
writing splinter.egg-info\PKG-INFO
writing dependency_links to splinter.egg-info\dependency_links.txt
writing requirements to splinter.egg-info\requires.txt
writing top-level names to splinter.egg-info\top_level.txt
reading manifest file 'splinter.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'tests'
writing manifest file 'splinter.egg-info\SOURCES.txt'
       3.执行命令python setup.py install

D:\splinter-0.7.7\splinter-0.7.7>python setup.py install
running install
running bdist_egg
running egg_info
writing splinter.egg-info\PKG-INFO
writing dependency_links to splinter.egg-info\dependency_links.txt
writing requirements to splinter.egg-info\requires.txt
writing top-level names to splinter.egg-info\top_level.txt
reading manifest file 'splinter.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'tests'
writing manifest file 'splinter.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
creating build\bdist.win32
creating build\bdist.win32\egg
creating build\bdist.win32\egg\splinter
copying build\lib\splinter\browser.py -> build\bdist.win32\egg\splinter
copying build\lib\splinter\cookie_manager.py -> build\bdist.win32\egg\splinter
creating build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\driver\djangoclient.py -> build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\driver\element_present.py -> build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\driver\flaskclient.py -> build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\driver\lxmldriver.py -> build\bdist.win32\egg\splinter\driver
creating build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\chrome.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\cookie_manager.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\firefox.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\phantomjs.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\remote.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\webdriver\__init__.py -> build\bdist.win32\egg\splinter\driver\webdriver
copying build\lib\splinter\driver\zopetestbrowser.py -> build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\driver\__init__.py -> build\bdist.win32\egg\splinter\driver
copying build\lib\splinter\element_list.py -> build\bdist.win32\egg\splinter
copying build\lib\splinter\exceptions.py -> build\bdist.win32\egg\splinter
copying build\lib\splinter\meta.py -> build\bdist.win32\egg\splinter
creating build\bdist.win32\egg\splinter\request_handler
copying build\lib\splinter\request_handler\status_code.py -> build\bdist.win32\egg\splinter\request_handler
copying build\lib\splinter\request_handler\__init__.py -> build\bdist.win32\egg\splinter\request_handler
copying build\lib\splinter\utils.py -> build\bdist.win32\egg\splinter
copying build\lib\splinter\__init__.py -> build\bdist.win32\egg\splinter
byte-compiling build\bdist.win32\egg\splinter\browser.py to browser.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\cookie_manager.py to cookie_manager.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\djangoclient.py to djangoclient.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\element_present.py to element_present.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\flaskclient.py to flaskclient.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\lxmldriver.py to lxmldriver.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\chrome.py to chrome.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\cookie_manager.py to cookie_manager.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\firefox.py to firefox.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\phantomjs.py to phantomjs.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\remote.py to remote.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\webdriver\__init__.py to __init__.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\zopetestbrowser.py to zopetestbrowser.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\driver\__init__.py to __init__.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\element_list.py to element_list.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\exceptions.py to exceptions.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\meta.py to meta.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\request_handler\status_code.py to status_code.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\request_handler\__init__.py to __init__.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\utils.py to utils.cpython-36.pyc
byte-compiling build\bdist.win32\egg\splinter\__init__.py to __init__.cpython-36.pyc
creating build\bdist.win32\egg\EGG-INFO
copying splinter.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying splinter.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying splinter.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO
copying splinter.egg-info\requires.txt -> build\bdist.win32\egg\EGG-INFO
copying splinter.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\splinter-0.7.7-py3.6.egg' and adding 'build\bdist.win32\egg' to it
removing 'build\bdist.win32\egg' (and everything under it)
Processing splinter-0.7.7-py3.6.egg
Copying splinter-0.7.7-py3.6.egg to d:\program files\python36-32\lib\site-packages
Adding splinter 0.7.7 to easy-install.pth file

Installed d:\program files\python36-32\lib\site-packages\splinter-0.7.7-py3.6.egg
Processing dependencies for splinter==0.7.7
Searching for selenium>=3.4.3
Reading https://pypi.python.org/simple/selenium/
Downloading https://pypi.python.org/packages/1a/7d/1e4aadfffc9c0de596839347e29a799c4124987ec6cdb3bcb46fa1970ffa/selenium-3.8.0.t
Best match: selenium 3.8.0
Processing selenium-3.8.0.tar.gz
Writing C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-21p0zp49\selenium-3.8.0\setup.cfg
Running selenium-3.8.0\setup.py -q bdist_egg --dist-dir C:\Users\ADMINI~1\AppData\Local\Temp\easy_install-21p0zp49\selenium-3.8.
warning: no files found matching 'selenium\selenium.py'
warning: no files found matching 'LICENSE'
creating d:\program files\python36-32\lib\site-packages\selenium-3.8.0-py3.6.egg
Extracting selenium-3.8.0-py3.6.egg to d:\program files\python36-32\lib\site-packages
Adding selenium 3.8.0 to easy-install.pth file

Installed d:\program files\python36-32\lib\site-packages\selenium-3.8.0-py3.6.egg
Finished processing dependencies for splinter==0.7.7
         3.验证Splinter是否安装成功

            在python模式下输入命令:from splinter.browser import Browser,如果没有报错,则说明安装成功

第三,执行py源码(https://github.com/fmc088/python12306.git)

        1.这里要下载chromedriver驱动文件

         chromedriverchrome映射表,参考文章:http://blog.csdn.net/huilan_same/article/details/51896672

         chromedriver下载地址:https://chromedriver.storage.googleapis.com/index.html

         这里要翻墙下载chromedriver,介绍几款翻墙软件地址:

                      https://github.com/getlantern/forum/issues/4775

                      https://github.com/getlantern/forum

 

         2 执行python D:\12306.py 自动下订单,不足点验证码要手动输入。 

D:\splinter-0.7.7\splinter-0.7.7>python D:\12306.py

 

DevTools listening on ws://127.0.0.1:12024/devtools/browser/fb7e0e32-892d-4a6a-98fe-4e0b38c94345

[0103/155600.945:ERROR:adm_helpers.cc(62)] Failed to querystereo recording.

等待验证自行输入...

[2100:5380:0103/155624.890:ERROR:audio_manager_base.cc(275)]Invalid audio output parameters received; using fake audio path. Channels: 0,Sample Rate: 0, Bits Per S

购票页面开始...

[2100:5380:0103/155625.437:ERROR:audio_manager_base.cc(275)]Invalid audio output parameters received; using fake audio path. Channels: 0,Sample Rate: 0, Bits Per S

循环点击查询... 第 1 次

Message: stale element reference: element is not attached to thepage document

  (Session info:chrome=62.0.3202.75)

  (Driver info:chromedriver=2.34.522940(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1x86)

 

还没开始预订 1

开始预订...

开始选择用户...

提交订单...

确认选座...  

             3.代码中要有地址cookies值,查找方法:

              starts = u"%u6DF1%u5733%u5317%2CIOQ"
               ends = u"%u53A6%u95E8%u5317%2CXKS"

              打开浏览器,按下F12 开发者模式,在出发地,目的地,开发者模式下就有对应的_jc_save_fromStation和_jc_save_toStationvalue值就是地址cookies



             

阅读更多
博主设置当前文章不允许评论。

没有更多推荐了,返回首页