发一个手动输入验证码自动补充账号密码登录12306的东东[含源码]
非程序猿工作的业余爱好者……学了大半年没写点东西……
实现的效果是:手动输入验证码,不停的登录,以便登入订票后台……...
简单功能……高手见笑……
1.使用VS2010开发
2.需要引用MSHTML(Microsoft HTML Object Library)
源码链接:http://download.csdn.net/detail/geminizane/4001304
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;
namespace winFormYZM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static int TestCount = 1;//尝试次数
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textBox1.Text = webBrowser1.Document.Url.ToString();//文本框显示为当前地址
timer1.Start();//启动显示时间的计时器
textBox2.Focus();//每次提交后,页面都会重新载入,因此,将验证码的输入框获得焦点,以便输入
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri("https://dynamic.12306.cn/otsweb/main.jsp"));//载入登录地址
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox3.Text) && !string.IsNullOrEmpty(textBox4.Text))
{
int intTxt = textBox2.Text.Length;
if (intTxt >= 5)//验证码为四位数,在输完四位数后,空格,增加一位,触发以下事件
{
string strCode = textBox2.Text.TrimEnd();//因为加了空格,所以去除末位的空格
webBrowser1.Document.Window.Frames[0].Document.GetElementById("UserName").SetAttribute("value", textBox3.Text);//账号输入框
webBrowser1.Document.Window.Frames[0].Document.GetElementById("password").SetAttribute("value", textBox4.Text);//密码输入框
webBrowser1.Document.Window.Frames[0].Document.GetElementById("randCode").SetAttribute("value", strCode);//验证码输入框
//webBrowser1.Document.Window.Frames[0].Document.InvokeScript("subForm();");
//webBrowser1.Document.Window.Frames[0].Document.GetElementById("subLink").InvokeMember("subForm");
HtmlElement str = webBrowser1.Document.Window.Frames[0].Document.GetElementById("subLink");//提交按钮
str.InvokeMember("click");//点击事件
textBox2.Text = "";//清空验证码文本框
label6.Text = "当前尝试次数:" + TestCount;//显示尝试次数
TestCount++;//尝试次数自增
}
}
else
{
MessageBox.Show("用户名/密码不能为空");
textBox2.Text = "";
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();//点击图片,刷新页面
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();//点击按钮,刷新页面
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//是否显示验证码
{
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
return;
}
//this.pictureBox1.Image = GetRegCodePic(ref webBrowser1, "img_rrand_code", null, null);
this.pictureBox1.Image = GetRegCodePic(ref webBrowser1, "img_rrand_code", null, null);
}
else
{
pictureBox1.Image = null;
}
}
private void timer1_Tick(object sender, EventArgs e)//显示当前时间的计时器
{
label5.Text ="当前时间:"+DateTime.Now.ToString();
}
//感谢.NET交流群(67784580)中的各位朋友的源码倾情提供
public static Image GetRegCodePic(ref WebBrowser webBrowser, String imgID, String imgSrc, String imgAlt)
{
//HTMLDocument doc = (HTMLDocument)webBrowser.Document.DomDocument;
HTMLDocument doc = (HTMLDocument)webBrowser.Document.Window.Frames[0].Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement img;
// 如果没有图片的ID,通过Src或Alt中的关键字来取
if (imgID.Length == 0)
{
Int32 ImgNum = GetPicIndex(ref webBrowser, ref imgSrc, ref imgAlt);
if (ImgNum == -1)
return null;
//img = (IHTMLControlElement)webBrowser.Document.Images[ImgNum].DomElement;
img = (IHTMLControlElement)webBrowser.Document.Window.Frames[0].Document.Images[ImgNum].DomElement;
}
else
{
//img = (IHTMLControlElement)webBrowser.Document.All[imgID].DomElement;
img = (IHTMLControlElement)webBrowser.Document.Window.Frames[0].Document.All[imgID].DomElement;
}
rang.add(img);
rang.execCommand("Copy", false, null);
Image regImg = Clipboard.GetImage();
Clipboard.Clear();
return regImg;
}
public static Int32 GetPicIndex(ref WebBrowser webBrowser, ref String imgSrc, ref String imgAlt)
{
IHTMLImgElement img;
// 获取所有的Image元素
//for (Int32 i = 0; i < webBrowser.Document.Images.Count; i++)
for (Int32 i = 0; i < webBrowser.Document.Window.Frames[0].Document.Images.Count; i++)
{
//img = (IHTMLImgElement)webBrowser.Document.Images[i].DomElement;
img = (IHTMLImgElement)webBrowser.Document.Window.Frames[0].Document.DomDocument;
if (imgAlt.Length == 0)
{
if (img.src.IndexOf(imgSrc) >= 0)
return i;
}
else
{
if (imgSrc.Length == 0)
{
// 当imgSrc为空时,只匹配imgAlt
if (img.alt.IndexOf(imgAlt) >= 0)
return i;
}
else
{
// 当imgSrc不为空时,匹配imgAlt和imgSrc任意一个
if (img.alt.IndexOf(imgAlt) >= 0 || img.src.IndexOf(imgSrc) >= 0)
return i;
}
}
}
return -1;
}
}
}
相关推荐
Python+Selenium登录12306练习
# login12306_02 # 图像识别涉及到深度学习,这里直接将验证码识别任务发送到大佬的验证码解析地址,不过现在已经失效了,程序跑到这会报错。 # 用户名和密码存储在本地工作目录中的 username_password_12306.txt文件中。 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriver
Python实现12306抢票助手[源码]
本文详细介绍了如何使用Python和Selenium库实现12306抢票助手。文章首先概述了代码的整体结构,包括必要的库导入和配置类定义。随后,详细解析了主要函数的功能,如输入行程信息、登录(支持扫码和账号密码两种方式)、查询车票以及执行抢票操作等。代码通过Selenium自动化操作12306网站,实现了从登录到购票的全流程。此外,文章还提供了代码的使用步骤,包括配置信息填写、必要库安装、浏览器驱动配置以及运行代码的具体方法。最后,强调了使用自动化工具时的合法性问题、验证码处理以及网络和服务器状况对抢票成功的影响。
Python小Demo实例源码12306
Python小Demo实例源码12306
易语言源码易语言POST12306源码.rar
易语言源码易语言POST12306源码.rar
Python3编写的各种大小程序,包含从零学Python系列、12306抢票、省市区地址库以及系列网站爬虫等学习源码.zip
Python 小脚本,Python版本为3.6很好用的代理推荐-SmartProxy最近发现了一个很好用的代理,给朋友们推荐一下SmartProxy主打1亿真实住宅IP资源,专业海外http代理商,千万级优质资源,覆盖全球城市,高匿稳定提供100%原生住宅IP,支持社交账户、电商平台、网络数据收集等服务。伪装度很高!本人测试用过之后感觉速度确实嘎嘎快,很给力。跨境电商的用户还可以用来tiktok养号,高速解决ip限制问题,可以注册多个账号,高效隐藏用户信息。付费套餐选择多样,现在春季价格优惠,动态住宅代理只要65折!如有这方面需求的,可以注册后联系客服,实名注册后赠送500M流量,不懂怎么用的朋友可以联系客服,中文客服24小时在线,或者看官网的视频教程也行,很多很详细。官网链接https://www.smartproxy.cn专属注册链接https://www.smartproxy.cn/regist?invite=7M2ZVK如何安装需要的模块一些第三方模块,需要自行安装,例如安装pymysql,安装方式如下pip install pymysql


