資源簡介
通過python在12306非常完整的下單購票程序、自動識別驗證碼登陸,有興趣且有python基礎的朋友可下載。下載后請讀代碼,創配置文件方可運行。由于最新更新的cookies存在,所以需要提前使用谷歌瀏覽器訪問12306官網才可使用該腳本。童叟無欺承諾后續跟新及周五定時查看留言問題,對得起你的積分
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?json?requests?ssl?os?datetime?urllib.parse?sys?sqlite3time
from?win32.win32crypt?import?CryptUnprotectData
from?selenium?import?webdriver
from?selenium.webdriver.chrome.options?import?Options
ssl._create_default_https_context?=?ssl._create_unverified_context
from?requests.packages.urllib3.exceptions?import?InsecureRequestWarning
#?禁用安全請求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#?構建SESSION
session?=?requests.session()
session.verify?=?False
header?=?{
????‘Host‘:?‘kyfw.12306.cn‘
????‘Referer‘:?‘https://kyfw.12306.cn/otn/resources/login.html‘
????‘User-Agent‘:?‘Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML?like?Gecko)?Chrome/74.0.3729.169?Safari/537.36‘
}
def?ma():
????import?re
????######查找對應網站的cookies
????host?=?‘.12306.cn‘
????cookiepath?=?os.environ[
?????????????????????‘LOCALAPPDATA‘]?+?r“\Google\Chrome\User?Data\Default\Cookies“??###C:\Users\xxxy用戶\AppData\Local\Google\Chrome\User?Data\Defaul
????sql?=?“select?host_keynameencrypted_value?from?cookies?where?host_key=‘%s‘“?%?host
????with?sqlite3.connect(cookiepath)?as?conn:
????????cu?=?conn.cursor()
????????cookies?=?{name:?CryptUnprotectData(encrypted_value)[1].decode()?for?host_key?name?encrypted_value?in
???????????????????cu.execute(sql).fetchall()}
????????#?print(cookies)
????????EXP?=?cookies[‘RAIL_EXPIRATION‘]??###打印對應的cookies值
????????RA?=?cookies[‘RAIL_DEVICEID‘]??###打印對應的cookies值
????print(RA)
????#?session.cookies[‘RAIL_EXPIRATION‘]?=EXP
????session.cookies[‘RAIL_DEVICEID‘]?=?RA
????#############################獲取驗證碼圖片########################
????import?base64configparser
????tu?=?“https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&1560324897958&callback=jQuery191046088946883591486_1560324572504&_=1560324572506“
????req?=?session.get(tu?headers=header)
????req?=?req.text
????img_base64?=?re.findall(r‘“image“:“(.*?)“‘?req)[0]
????img_encode?=?base64.b64decode(img_base64)
????with?open(‘tu.jpg‘?‘wb‘)?as?f:
????????f.write(img_encode)
????##########################用戶輸入#############################
????today?=?datetime.date.today()
????config?=?configparser.ConfigParser()
????path?=?“12306.txt“
????config.read(path?encoding=“utf-8-sig“)
????user?=?config.get(“info“?“user“)
????password?=?config.get(“info“?“password“)
????cf?=?config.get(“info“?“cf“)
????zd?=?config.get(“info“?“zd“)
????time?=?config.get(“info“?“time“)
????mail?=?config.get(“info“?“mail“)
????stat_time?=?config.get(“info“?“cartime“)
????stat_time?=?stat_time+“:“
????zuo?=?config.get(“info“?“zuo“)
????people?=?config.get(“info“?“people“)
????##########################驗證碼輸入#############################
????dcit?=?{‘1‘:?‘4152‘?‘2‘:?‘11050‘?‘3‘:?‘17850‘?‘4‘:?‘25552‘
????????????‘5‘:?‘41117?‘?‘6‘:?‘111122‘?‘7‘:?‘182125‘?‘8‘:?‘252117‘}
????url?=?“http://shell.teachx.cn:12306/predict“
??
- 上一篇:老唐的1——python數據分析與機器學習實戰
- 下一篇:百度搜索批量爬取真實URL
評論
共有 條評論