以下代码可自动登录12306 - 包括输入用户名密码以及自动识别验证码并点击验证码登陆。该源码需要稍作修改:
把 username.send_keys('xxxxxxx') 中的 xxxxxx 改为 你自己的12306账号。
把 password.send_keys('yyyyyy') 中的 yyyyy 改为自己的 12306 密码。
即可运行。
该源码把自动抢票的核心功能:识别验证码并点击验证码登陆实现了。
把代码稍作加工,即可变为自己的自动抢票代码。
运行环境 - 需要安装python运行环境,selenium,requests,浏览器默认为chrome。
运行时 程序会自动分析并识别验证码并点击验证码,完成登陆过程。。。
详细代码如下:
#12306 自动打开12306网站,并输入用户名、密码和验证码,并登录12306,
#author bigluo
#email: 3490699170@qq.com
#coding=utf-8
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver.common.action_chains import ActionChains
import os
import requests
import numpy
#指定button id和button文本值,并点击,连续点击5次
#return:
#0 click successfully
#-1 连续5次均failed
#1 txt != dest_text,所以不点击
def click_button(b,id,dest_text,j): #在当前页面查找并点击指定text,错误返回 -1.连续5次,错误时延时1秒
txt=''
for i in range(0,5):
try:
txt=b.find_element_by_id(id).text
if txt == dest_text:
b.find_element_by_id(id).click()
return 0
else:
return 1
except:
time.sleep(1)
continue
return -1 #5次都失败了
#给定button id和text,find a given text
把 username.send_keys('xxxxxxx') 中的 xxxxxx 改为 你自己的12306账号。
把 password.send_keys('yyyyyy') 中的 yyyyy 改为自己的 12306 密码。
即可运行。
该源码把自动抢票的核心功能:识别验证码并点击验证码登陆实现了。
把代码稍作加工,即可变为自己的自动抢票代码。
运行环境 - 需要安装python运行环境,selenium,requests,浏览器默认为chrome。
运行时 程序会自动分析并识别验证码并点击验证码,完成登陆过程。。。
详细代码如下:
#12306 自动打开12306网站,并输入用户名、密码和验证码,并登录12306,
#author bigluo
#email: 3490699170@qq.com
#coding=utf-8
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver.common.action_chains import ActionChains
import os
import requests
import numpy
#指定button id和button文本值,并点击,连续点击5次
#return:
#0 click successfully
#-1 连续5次均failed
#1 txt != dest_text,所以不点击
def click_button(b,id,dest_text,j): #在当前页面查找并点击指定text,错误返回 -1.连续5次,错误时延时1秒
txt=''
for i in range(0,5):
try:
txt=b.find_element_by_id(id).text
if txt == dest_text:
b.find_element_by_id(id).click()
return 0
else:
return 1
except:
time.sleep(1)
continue
return -1 #5次都失败了
#给定button id和text,find a given text