add 12306

This commit is contained in:
CharlesPikachu
2019-03-05 18:31:36 +08:00
parent 5fd31671b6
commit 832abd5493
5 changed files with 112 additions and 3 deletions
+4 -1
View File
@@ -9,7 +9,7 @@ Author:
GitHub:
https://github.com/CharlesPikachu
更新日期:
2019-03-04
2019-03-05
'''
try:
from .platforms import *
@@ -33,6 +33,9 @@ class Login():
'''网易云音乐'''
def music163(self, username, password, version='pc'):
return music163.music163().login(username, password, version)
'''12306'''
def zt12306(self, username, password, version='pc'):
return zt12306.zt12306().login(username, password, version)
'''Info'''
def __repr__(self):
return self.info
+1 -1
View File
@@ -1 +1 @@
__all__ = ['douban', 'github', 'weibo', 'music163']
__all__ = ['douban', 'github', 'weibo', 'music163', 'zt12306']
+102
View File
@@ -0,0 +1,102 @@
# coding: utf-8
'''
Function:
12306模拟登录
--https://www.12306.cn/index/(PC端)
--移动端暂不支持
Author:
Charles
微信公众号:
Charles的皮卡丘
GitHub:
https://github.com/CharlesPikachu
更新日期:
2019-03-05
'''
import os
import time
import requests
from PIL import Image
'''
Function:
12306模拟登录
Detail:
-login:
Input:
--username: 用户名
--password: 密码
--version: mobile/pc
Return:
--session: 登录后的requests.Session()
'''
class zt12306():
def __init__(self, **kwargs):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
self.positions = ['36,46', '109,44', '181,47', '254,44', '33,112', '105,116', '186,116', '253,115']
self.vcode_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.5579044251920726'
self.verify_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
self.login_url = 'https://kyfw.12306.cn/passport/web/login'
self.cur_path = os.path.abspath(os.path.dirname(__file__))
self.session = requests.Session()
'''登录函数'''
def login(self, username, password, version='pc'):
if version == 'mobile':
return None
elif version == 'pc':
self.__downloadVcode()
time.sleep(0.1)
res = self.__verifyVcode()
if not res:
raise RuntimeError('verification code error...')
data = {
'username': username,
'password': password,
'appid': 'otn'
}
res = self.session.post(self.login_url, headers=self.headers, data=data)
if res.status_code == 200:
print('[INFO]: Account -> %s, login successfully...' % username)
return self.session
else:
raise RuntimeError('Account -> %s, fail to login, username or password error...' % username)
else:
raise ValueError('Unsupport argument in zt12306.login -> version %s, expect <mobile> or <pc>...' % version)
'''下载验证码'''
def __downloadVcode(self):
res = self.session.get(self.vcode_url, headers=self.headers)
with open(os.path.join(self.cur_path, 'vcode.jpg'), 'wb') as f:
f.write(res.content)
return True
'''验证码验证'''
def __verifyVcode(self):
img_path = os.path.join(self.cur_path, 'vcode.jpg')
img = Image.open(img_path)
img.show()
user_enter = input('Enter the positions of verification code, use <,> to separate, such as <2,3>\n(From left to right, top to bottom -> 1,2,3,4,5,6,7,8):')
verify_list = []
for each in user_enter.split(','):
each = each.strip()
try:
verify_list.append(self.positions[int(each)-1])
except:
raise RuntimeError('verification code error...')
data = {
'answer': ','.join(verify_list),
'login_site': 'E',
'rand': 'sjrand'
}
res = self.session.post(url=self.verify_url, headers=self.headers, data=data)
os.remove(img_path)
if res.json()['result_code'] == '4':
return True
else:
return False
'''test'''
if __name__ == '__main__':
zt12306().login('', '')
+4
View File
@@ -23,6 +23,9 @@ You can star this repository to keep track of the project if it's helpful for yo
#### Music163
- [x] [PC](https://music.163.com/) → in src/platforms/music163.py
- [ ] mobile
#### 12306
- [x] [PC](https://www.12306.cn/index/) → in src/platforms/zt12306.py
- [ ] mobile
# Install
### Use setup.py
@@ -48,6 +51,7 @@ session = l.douban(username[telephone], password, 'pc')
session = l.github(username[email], password, 'pc')
session = l.weibo(username[telephone], password, 'mobile')
session = l.music163(username[telephone/email], password, 'pc')
session = l.zt12306(username[telephone], password, 'pc')
```
#### Explain
```sh
+1 -1
View File
@@ -29,7 +29,7 @@ setup(
author_email='charlesjzc@qq.com',
license='MIT',
include_package_data=True,
install_requires=['requests'],
install_requires=['requests', 'pycryptodome', 'pillow'],
zip_safe=True,
packages=find_packages()
)