买票难?--使用Github开源工具
注:坑还没踩完
注:到目前为止这是一篇宣告失败的博客
最近翻到了一篇大佬的博客Github上的开源工具帮助你实现“十一”回家的愿望,又赶上中秋节和国庆,很多人想回家都面临着买票的难题.
虽然我不会python,但今天就来试一试这个工具如何使用.
特别说明:本项目属于爬虫类项目,如果商用可能会有法律风险,请各位读者谨慎使用,如果由于根据本文使用12306自动抢票软件引发法律纠纷,笔者并不承担。
我使用的平台是centos7-minimal,默认python是2.7.5,但这个工具需要3.6及以上版本,所以要先升个级
用户为root,如果你是普通用户遇到了权限问题,请使用su root
1.Python版本升级
查看版本
[root@hadoop03 ~]# python -V
Python 2.7.5
下载编译
为了不对一些以来此版本python的工具造成影响,我们选择只下载新版本,不删除旧版本
yum install gcc -y
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar -zxvf Python-3.7.4.tgz
cd Python-3.7.4
./configure
make -j4 & make -j4 install
如果你和我一样是centos7-minimal可能会遇到下面的错误,那就先添加完依赖再执行make & make install
嫌慢使用make -j4 & make -j4 install速度会变快(4个cpu一起)
遇到错误:zipimport.ZipImportError: can’t decompress data
解决: yum -y install zlib*
遇到错误:ModuleNotFoundError: No module named ‘_ctypes’
解决: yum install libffi-devel -y
验证安装成功
[root@hadoop03 Python-3.7.4]# python -V
Python 2.7.5
[root@hadoop03 Python-3.7.4]# python3 -V
Python 3.7.4
更改默认python
[root@hadoop03 ~]# ll /usr/bin | grep python
lrwxrwxrwx. 1 root root 7 8月 20 20:31 python -> python2
lrwxrwxrwx. 1 root root 9 8月 20 20:31 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 6月 21 04:28 python2.7
将原来 python 的软链接重命名:
mv /usr/bin/python /usr/bin/python.bak
将 python 链接至 python3:
ln -s /usr/local/bin/python3 /usr/bin/python
配置yum
升级 Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用,需要编辑 yum 的配置文件,此时:
[root@hadoop03 ~]# yum list
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down,将 #!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可。
2.下载项目源码
安装GIT
yum -y install git
克隆
git clone https://github.com/testerSunshine/12306.git
cd 12306
下载项目依赖的识别码打码模型
打开以下网址:https://github.com/testerSunshine/12306model,下载下图两个标红的模型文件,并放到12306项目的根目录(一般是~/12306)
我的做法是
git clone https://github.com/testerSunshine/12306model.git
下载时间可能有2分钟
然后把12306model目录下的两个目标文件移动到12306下
安装项目依赖
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
遇到了错误
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting beautifulsoup4==4.5.3 (from -r requirements.txt (line 1))
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/beautifulsoup4/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/beautifulsoup4/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/beautifulsoup4/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/beautifulsoup4/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/beautifulsoup4/
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/beautifulsoup4/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/beautifulsoup4/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Could not find a version that satisfies the requirement beautifulsoup4==4.5.3 (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for beautifulsoup4==4.5.3 (from -r requirements.txt (line 1))
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
尝试解决
解决方法:
在./configure过程中,如果没有加上–with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,
刚好pip3过程需要ssl模块,而由于没有指定,所以该功能不可用。
查看openssl安装包,发现缺少openssl-devel包
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-20.el5
openssl-0.9.8e-20.el5
[root@localhost ~]#
yum安装openssl-devel
[root@localhost ~]# yum install openssl-devel -y
查看安装结果
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-26.el5_9.1
openssl-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1
重新对python3.7.2进行编译安装,用以下过程来实现编译安***r> cd Python-3.7.2
./configure --with-ssl
make
sudo make install
执行
[root@hadoop03 Python-3.7.4]# yum install openssl-devel -y
[root@hadoop03 Python-3.7.4]# ./configure --with-ssl
[root@hadoop03 Python-3.7.4]# make -j4 & make -j4 install
继续执行遇到错误
[root@hadoop03 Python-3.7.4]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
Could not open requirements file: [Errno 2] 没有那个文件或目录: 'requirements.txt'
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
执行
[root@hadoop03 Python-3.7.4]# pip3 install --upgrade pip
切换到12306目录下
[root@hadoop03 12306]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
然后吨吨吨的给我下载了一堆东西.
安装chromedriver
使用以下命令安装chromedriver,一般安装后会放在/usr/local/bin下,需要在配置文件的CHROME_PATH标签下填好
sudo apt-get install chromium-chromedriver
因为大佬使用的是ubuntu,而我是centos,.所以我使用yum试试
执行后
很凌乱,我现在很后悔,我一个没有界面的centos7…
而且这些乱码不只是这个命令下,咋回事…
断开xshell连接,重新连接就没有乱码了…
我又来头铁了
这一次参考如下
CentOS7下无界面使用Selenium+chromedriver进行自动化测试
linux无界面模式安装selenium+chrome+chromedriver并成功完成脚本
1.安装selenium
pip3 install selenium
提示我Requirement already satisfied: selenium in /usr/local/lib/python3.7/site-packages (3.11.0),已经安好了
2.安装chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
3.查看自己的chrome版本
[root@hadoop03 ~]# yum list | grep chrome
google-chrome-stable.x86_64 76.0.3809.132-1 installed
google-chrome-beta.x86_64 77.0.3865.75-1 google-chrom
google-chrome-unstable.x86_64 78.0.3902.4-1 google-chrom
xorg-x11-drv-openchrome.i686 0.5.0-3.el7.1 base
xorg-x11-drv-openchrome.x86_64 0.5.0-3.el7.1 base
xorg-x11-drv-openchrome-devel.i686 0.5.0-3.el7.1 base
xorg-x11-drv-openchrome-devel.x86_64 0.5.0-3.el7.1 base
下载chrome-driver 去官网找到对应的chrome版本 https://npm.taobao.org/mirrors/chromedriver
wget https://npm.taobao.org/mirrors/chromedriver/76.0.3809.126/chromedriver_linux64.zip
解压
yum install -y unzip zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin/
修改配置文件
# 关于软件使用配置说明,一定要看!!!
# 刷票模式:1=刷票 2=候补+刷票
TICKET_TYPE = 2
# 出发日期(list) "2018-01-06", "2018-01-07"
STATION_DATES = [
"2019-09-12"
]
# 填入需要购买的车次(list),"G1353"
STATION_TRAINS = [
"G471",
]
# 出发城市,比如深圳北,就填深圳就搜得到
FROM_STATION = "北京南"
# 到达城市 比如深圳北,就填深圳就搜得到
TO_STATION = "淄博"
# 座位(list) 多个座位ex:
# "商务座",
# "一等座",
# "二等座",
# "特等座",
# "软卧",
# "无座",
# "动卧",
SET_TYPE = [
"硬座",
]
# 当余票小于乘车人,如果选择优先提交,则删减联系人和余票数一致在提交
# bool
IS_MORE_TICKET = True
# 乘车人(list) 多个乘车人ex:
# - "张三"
# - "李四"
TICKET_PEOPLES = [
# 12306登录账号
USER = ""
PWD = ""
# 加入小黑屋时间默认为5分钟,此功能为了防止僵尸票导致一直下单不成功错过正常的票
TICKET_BLACK_LIST_TIME = 5
# 自动打码
# 邮箱配置,如果抢票成功,将通过邮件配置通知给您
# 列举163
# username: "xxxxx"
# password: "xxxxx
# host: "smtp.163.com"
# email: "xxx@qq.com"
# notice_email_list: "123@qq.com"
# username: "xxxxx"
# password: "授权码"
# host: "smtp.qq.com"
EMAIL_CONF = {
"IS_MAIL": True,
"email": "",
"notice_email_list": "",
"username": "",
"password": "",
SERVER_CHAN_CONF = {
"is_server_chan": False,
"secret": ""
}
# 是否开启cdn查询,可以更快的检测票票 1为开启,2为关闭
IS_CDN = 1
ORDER_TYPE = 2
# 2 是捡漏,捡漏的刷新间隔时间为0.5-3秒,时间间隔长,不容易封ip
ORDER_MODEL = 2
# 关于软件使用配置说明,一定要看!!!
# 刷票模式:1=刷票 2=候补+刷票
TICKET_TYPE = 2
# 出发日期(list) "2018-01-06", "2018-01-07"
STATION_DATES = [
"2019-09-12"
]
# 填入需要购买的车次(list),"G1353"
STATION_TRAINS = [
"G471",
]
# 出发城市,比如深圳北,就填深圳就搜得到
FROM_STATION = "北京南"
# 到达城市 比如深圳北,就填深圳就搜得到
TO_STATION = "淄博"
# 座位(list) 多个座位ex:
# "商务座",
# "一等座",
# "二等座",
# "特等座",
# "软卧",
# "无座",
# "动卧",
SET_TYPE = [
"硬座",
]
# 当余票小于乘车人,如果选择优先提交,则删减联系人和余票数一致在提交
# bool
IS_MORE_TICKET = True
# 乘车人(list) 多个乘车人ex:
# - "张三"
# - "李四"
TICKET_PEOPLES = [
# 12306登录账号
USER = ""
PWD = ""
# 加入小黑屋时间默认为5分钟,此功能为了防止僵尸票导致一直下单不成功错过正常的票
TICKET_BLACK_LIST_TIME = 5
# 自动打码
# 邮箱配置,如果抢票成功,将通过邮件配置通知给您
# 列举163
# username: "xxxxx"
# password: "xxxxx
# host: "smtp.163.com"
# email: "xxx@qq.com"
# notice_email_list: "123@qq.com"
# username: "xxxxx"
# password: "授权码"
# host: "smtp.qq.com"
EMAIL_CONF = {
"IS_MAIL": True,
"email": "",
"notice_email_list": "",
"username": "",
"password": "",
SERVER_CHAN_CONF = {
"is_server_chan": False,
"secret": ""
}
# 是否开启cdn查询,可以更快的检测票票 1为开启,2为关闭
IS_CDN = 1
# 下单接口分为两种,1 模拟网页自动捡漏下单(不稳定),2 模拟车次后面的购票按钮下单(
稳如老狗)
ORDER_TYPE = 2
# 下单模式 1 为预售,整点刷新,刷新间隔0.1-0.5S, 然后会校验时间,比如12点的预售,那>脚本就会在12.00整检票,刷新订单
# 2 是捡漏,捡漏的刷新间隔时间为0.5-3秒,时间间隔长,不容易封ip
ORDER_MODEL = 2
# 是否开启代理, 0代表关闭, 1表示开始
# 开启此功能的时候请确保代理ip是否可用,在测试放里面经过充分的测试,再开启此功能,不
然可能会耽误你购票的宝贵时间
# 使用方法:
# 1、在agency/proxy_list列表下填入代理ip
# 2、测试UnitTest/TestAll/testProxy 测试代理是否可以用
# 3、开启代理ip
IS_PROXY = 0
# 预售放票时间, 如果是捡漏模式,可以忽略此操作
OPEN_TIME = "13:00:00"
# 1=使用selenium获取devicesID
# 2=使用网页端/otn/HttpZF/logdevice获取devicesId,这个接口的算法目前可能有点问题,如
果登录一直302的请改为配置1
COOKIE_TYPE = 1
# 如果COOKIE_TYPE=2,则需配置chromeDriver路径,下载地址http://chromedriver.storage.googleapis.com/index.html
# chromedriver配置版本只要和chrome的大版本匹配就行
CHROME_PATH = "/usr/local/bin/chromedriver"
运行程序
运行:python run.py
提示ImportError: libSM.so.6: cannot open shared object file: No such file or directory
使用以下命令解决:
yum install libSM-1.2.2-2.el7.x86_64 --setopt=protected_multilib=false
提示ImportError: /lib64/libstdc++.so.6: version `CXXABI_1.3.8’ not found (required by /usr/local/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
解决问题:
https://blog.csdn.net/u012811841/article/details/77854581/
但是我找不到libstdc++.so.6.0.24怎么下载,所有接下来尝试升级gcc
https://blog.csdn.net/lianshaohua/article/details/83411684
第一步
wget http://www.netgull.com/gcc/releases/gcc-8.2.0/gcc-8.2.0.tar.gz
第二步
tar -zxf gcc-8.2.0.tar.gz
第三步 到gcc-8.2.0目录执行
./contrib/download_prerequisites
第四步
./configure --prefix=/usr --enable-multilib --enable-languages=c,c++
接下来make
make两个多小时了…
make了n个小时,
终于到make install了,Σ(っ °Д °;)っ,噌噌噌就结束了
再次运行程序
报错:ImportError: /lib64/libm.so.6: version `GLIBC_2.23’ not found (required by /usr/local/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
缺啥下啥:wget https://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.gz
tar -zxvf glibc-2.23.tar.gz
cd glibc-2.23
mkdir build
cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
在make的时候又遇到了…
setenv.c: In function '__unsetenv':
setenv.c:279:6: error: suggest explicit braces to avoid ambiguous 'else' [-Werror=dangling-else]
if (ep != NULL)
^
cc1: all warnings being treated as errors
make[2]: *** [/export/home/lianglixin/Simpson_Setup_Linux_4.2.1/Simpson/glibc-2.23/build/stdlib/setenv.o] Error 1
make[2]: Leaving directory `/export/home/lianglixin/Simpson_Setup_Linux_4.2.1/Simpson/glibc-2.23/stdlib'
make[1]: *** [stdlib/subdir_lib] Error 2
make[1]: Leaving directory `/export/home/lianglixin/Simpson_Setup_Linux_4.2.1/Simpson/glibc-2.23'
make: *** [all] Error 2
解决:http://tieba.baidu.com/p/5697560265?traceid=
再次运行程序(⊙x⊙;)
怎么回事还是ImportError: /lib64/libm.so.6: version `GLIBC_2.23’ not found (required by /usr/local/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
睡了睡了…