c # realize crawl imei code information from other sites, manually enter the verification code

 

Read more: http://www.yzswyl.cn/blread-1603.html

 

Function : from other sites manually enter the verification code and grab the phone IMEI information

 

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 ;
                 }
             }
         }
     
     
     
     }
}

 

  

 

Controls :

 

pictureBox1

 

label1

 

textBox1

 

button1

 

button2

 

button3

 

pictureBox2

 

richTextBox1

 

Quote :

 

Download HtmlAgilityPack, add a reference, Download: http://htmlagilitypack.codeplex.com/  , click on download to download the right side

 

Effect :

 

 

Original: http://www.yzswyl.cn/blread-1603.html

 

Guess you like

Origin www.cnblogs.com/jjg0519/p/12610633.html