c# winform webBrowser爬取數據


int index=0;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)//是否完全加載完成
            {
                textBox2.AppendText("就緒.....\r\n");
                if (index == 0)
                {
                    if (e.Url.ToString() == webBrowser1.Url.ToString())
                    {
                        this.cobURL.Text = this.webBrowser1.Document.Url.ToString();
                        this.TabText.Text = this.webBrowser1.DocumentTitle;

                        HtmlDocument hd = webBrowser1.Document;
                        HtmlElement he = hd.GetElementsByTagName("input").GetElementsByName("keywords")[0];

                        he.InnerText = this.textBox6.Text.Trim().ToString();//你要查的內容
                        HtmlElement bt = webBrowser1.Document.All["header-search-button"];
                        bt.InvokeMember("click");//主頁面,觸發點擊事件,根據你要查的內容去進入子頁面
                        textBox2.AppendText("正在進入子頁面.....\r\n");
                        index++;
                    }
                }
                else if (index == 1)
                {
                    textBox2.AppendText("進入"+ e.Url.ToString() + "\r\n");
                    if (e.Url.ToString() == webBrowser1.Url.ToString())
                    {
                        this.cobURL.Text = this.webBrowser1.Document.Url.ToString();
                        this.TabText.Text = this.webBrowser1.DocumentTitle;

                        HtmlDocument hd = webBrowser1.Document;
                        if (hd.GetElementById("DatasheetsTable1")==null)
                        {
                            textBox2.AppendText("該地址未能找到所需數據.....\r\n");
                            textBox2.AppendText("獲取結束.....\r\n");
                            webBrowser1.DocumentCompleted -= new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
                            index = 0;
                            return;
                        }
                        string ceshi= hd.GetElementById("DatasheetsTable1").FirstChild.OuterText.ToString();
                        string[] sArray = ceshi.Split(new string[] { "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
                        string bz= sArray[1].Replace("c數據表頭", "");//根據情況處理
                        bz= bz.Trim().ToString();//獲取數據c
                        textBox3.Text = bz;

                        string ceshi1 = hd.GetElementById("SpecificationTable1").FirstChild.OuterText.ToString();
                        string[] sArray1 = ceshi1.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        string fz = sArray1[7].Replace("a數據表頭", ""); 
                        fz = fz.Trim().ToString();//獲取數據a
                        textBox4.Text = fz;
                        string qjfz = sArray1[8].Replace("b數據表頭", "");
                        qjfz = qjfz.Trim().ToString();//獲取數據b
                        textBox5.Text = qjfz;

                        index = 0;
                        textBox2.AppendText("獲取結束.....\r\n");
                        webBrowser1.DocumentCompleted -= new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
                    }
                }
                
                
            }
        }
private void button7_Click(object sender, EventArgs e)
        {
            if (textBox6.Text.ToString()!="")
            {
                textBox2.Clear();
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox2.AppendText("正在獲取中.....\r\n");
                this.webBrowser1.Url = new Uri("地址");
                this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
            }
            
        }

界面大致,webBrowser隱藏了,把webBrowser的ScriptErrorsSuppressed要設置為True,負責會彈script錯誤

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM