c#实现从其他网站抓取imei码信息,手工输入验证码

阅读全文:http://www.yzswyl.cn/blread-1603.html

功能:从其他网站手工输入验证码并抓取手机IMEI信息

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 System.Net;
using System.IO;
    
namespace getImei
{
    public partial class Form1 : Form
    {
        private String imgUrl;
        string url = "http://abc.aspx";//抓取的地址
        private WebBrowser wb = new WebBrowser();
        Uri baseUri;
        WebClient wc = new WebClient();
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            wb.Navigate(url);
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    
        }
    
        //重新获取验证码
        private void button1_Click(object sender, EventArgs e)
        {
             wb.Navigate(url);
             wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    
        }
    
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (wb.Document.Url == e.Url)
            {
                //加载完毕。
                HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                html.LoadHtml(wb.DocumentText);
                HtmlAgilityPack.HtmlNode htmlNode = html.DocumentNode;
                HtmlAgilityPack.HtmlNode tagImg = htmlNode.SelectSingleNode("//*[@id=\"SNTD\"]/img");
                if (tagImg != null)
                {
                    string imgReUri = tagImg.GetAttributeValue("src", "");
                    baseUri = new Uri(url);
                    Uri imgUri = new Uri(baseUri, imgReUri);
    
                    byte[] imgData = wc.DownloadData(imgUri);
    
                    pictureBox1.Image = BytesToImage(imgData);
                }
            }
            if (wb.Document.Url.ToString().Contains("Check/IMEIValidateDetail.aspx?a="))
            {
                //内容
                HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                html.LoadHtml(wb.DocumentText);
                HtmlAgilityPack.HtmlNode htmlNode = html.DocumentNode;
                HtmlAgilityPack.HtmlNode summary = htmlNode.SelectSingleNode("//table[@class=\"checkContent2\"]");
                if (summary != null)
                {
                    //webBrowser1.DocumentText = summary.InnerText;
                    richTextBox1.Text = summary.InnerHtml;
                }
    
                //图片
                HtmlAgilityPack.HtmlNode iframe = htmlNode.SelectSingleNode("//table[@class=\"checkContent2\"]/tr[1]/td[1]/iframe");
                if (iframe != null)
                {
                    string frameRes = iframe.GetAttributeValue("src", "无图片");
                    Uri frameUri = new Uri(baseUri, frameRes);
                    string iframePageData = wc.DownloadString(frameUri);
                    html.LoadHtml(iframePageData);
                    htmlNode = html.DocumentNode;
                    HtmlAgilityPack.HtmlNode imgNode = htmlNode.SelectSingleNode("//*[@id=\"imgEQ\"]");
                    if (imgNode != null)
                    {
                        string thumbRes = imgNode.GetAttributeValue("src", "");
                        Uri thumbUri = new Uri(baseUri, thumbRes);
                        pictureBox2.Load(thumbUri.ToString());
                    }
    
                }
            }
        }
    
        public static Image BytesToImage(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Image img = Image.FromStream(ms);
            return img;
        }
    
        //抓取IMEI内容
        private void button2_Click(object sender, EventArgs e)
        {
            wb.Document.GetElementById("ctl00$ContentPlaceHolder1$IMEICode").InnerText = "****************";//手机IMET号码
            wb.Document.GetElementById("ctl00_ContentPlaceHolder1_txtValidateCode").InnerText = textBox1.Text;
            wb.Document.GetElementById("ctl00_ContentPlaceHolder1_submit").InvokeMember("click");
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
        }
        //查看详细信息
        private void button3_Click(object sender, EventArgs e)
        {
            foreach (HtmlElement he in wb.Document.GetElementsByTagName("a"))
            {
                if ("(查看详细)".Equals(he.InnerText.Trim()))
                {
                    he.InvokeMember("click");
                    break;
                }
            }
        }
    
    
    
    }
}

  

控件

pictureBox1

label1

textBox1

button1

button2

button3

pictureBox2

richTextBox1

引用

下载HtmlAgilityPack,添加引用,下载地址:http://htmlagilitypack.codeplex.com/ ,点击右侧的download即可下载

效果

原文:http://www.yzswyl.cn/blread-1603.html

posted @ 2012-11-29 13:59  雪洁  阅读(955)  评论(0编辑  收藏  举报