WebBrowser是Visual Studio裡面的一個控制項

首先先把它拉到Form上吧

設定一下loading這個全域變數為true

bool loading = true;

寫程式在一個按鈕的事件裡

private void btn_Go_Click(object sender, EventArgs e)
{

              for (int I = 0; I <= 25; I++)
            {
                 loading = true;

                 //假設Google有http://www.google.com?Hello=A,http://www.google.com?Hello=B,http://www.google.com?Hello=C....等26個網頁

                 //就用迴圈去跑,當網頁讀進來完成之後,便會觸發到下面的Navigated事件

                 //(DocumentCompleted事件的話常常會有問題,因為如果網頁下載不完全就會當在那邊)
                 webBrowser1.Navigate("http://www.google.com?Hello="+ Convert.ToChar(65 + I));

                //跑到這因為loading還是true的關係,會一直跑下面這個迴圈

                //而Application.DoEvents();是讓程式在跑迴圈時還能去傾聽其他的事件
                while (loading)
                {
                    Application.DoEvents();
                }

            }

}

 

 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{

      //放個計時器,等待一下網頁下載回來
      Webtimer.Start();
}

 

//等待完後就開始擷取網頁資料

private void Webtimer_Tick_1(object sender, EventArgs e)
{
            Webtimer.Stop();

 

            //這邊使用的是一種叫做DOM的技術,DOM是Document Object Model          
            HtmlDocument doc = webBrowser1.Document;

            for (int i = 0; i < doc.All.Count; i++)
            {                  

                                 //去抓標籤名字有a的,像是連結<a href=xx>這種

                                 //奇怪的是這個方法如果要抓一些標籤都抓不到,像是<td><tr>那些=.=

                                 //所以那些我是用另外的方法抓的,等下會講
                if (doc.All[i].TagName.ToLower().Equals("a"))
                {             

                                       //GetAttribute就是去取得標籤的屬性的內容,例:

                                      //<a href ="我是屬性的內容" target=_blank>,不過有些屬性取不到,像是class

                    if (doc.All[i].GetAttribute("href").Contains("abc"))
                    {                             //InnerText就是取得標籤中間的內容,<標籤>內容</標籤>
                        richTextContent1.AppendText(doc.All[i].InnerText);
                        richTextContent1.AppendText(doc.All[i].GetAttribute("href"));
                        richTextContent1.AppendText(Environment.NewLine);

                      //另外這個函式可以去更改HTML裡面屬性的內容
              XXX.SetAttribute("value","Hello");

                    }
                }
           }

            loading = false;
}

 

另外還有些用法

 HtmlDocument doc2 = webBrowser2.Document;

//搜尋整個Body標籤內的內容是否有"我在這"這個文字字串

doc2.Body.InnerHtml.Contains("我在這")

 

//整個文件的Img標籤有幾個

doc2.Images.Count

 

//先搜尋標籤名字為tbody的標籤,然後後面的[4]代表著拿整個HTML的第四個tbody

HtmlElement tbody = doc2.GetElementsByTagName("tbody")[4];

 

//拿上面第4個tbody裡面的第6個tr標籤

HtmlElement tr = tbody.GetElementsByTagName("tr")[6];

 

//這是webbrowser1的"事件",當跳出新視窗時要做什麼

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

{
           //這行就是讓多跳出的新視窗跳不出來 =.="

           e.Cancel = true;
}

//讀Frames用法

HtmlWindow currentWindow = WebBrowser1.Document.Window;

HtmlWindow frame = currentWindow.Frames["Frame1"];
foreach (HtmlWindow frame in currentWindow.Frames)
{
     

}

 

//找到某一個按鈕的標籤,並且按下去

HtmlElement MyButton = Doc.Forms["main_form"].GetElementsByTagName("input")[3];

 

MyButton.InvokeMember("click");

 

 

 

 

 

 

 

arrow
arrow
    全站熱搜

    welkingunther 發表在 痞客邦 留言(15) 人氣()