數據採集實踐學習二(C#)

     前一篇文章寫到我獲取數據的方式不是經過分析HTML得到,而是經過分析請求連接,而後模擬請求方法獲取數據,這只是一種方法。並且是在我經過分析HTML獲取不到的狀況下,曲線救國,參考別人文章實現的。很高興,我實現了本身獲取數據的目標。我覺得這樣就算結束了。但是,今天又發現了另一種方法,並且是經過分析HTML實現的,看到它,我感受太難以想象了,我花了那麼多的時間都沒有實現,怎麼如今又能夠了。如今興趣正濃,趕忙操刀實踐一番。因而有了這篇,算是意外之喜吧!html

 

    先說明一下實現思路,原來它是經過調用WebBrowser控件來實現的。怪不得它能夠獲取HTML,而後分析獲取數據。管你什麼動態解析,ajax,如今我是瀏覽器行爲了,全部的都逃不過個人法眼。真的是不錯的選擇方式。

說明一下,包含三個地方。web


一個解析獲取解析HTML類,一個事件類,一個調用的地方。上次我是拿那個情趣網站實驗,結果你們都說我好污,好污,其實我是一個好人,一個讓你們都有動力興趣的好人,代碼寫累了,看看圖片,又雞血了,我不信你們對美圖不感興趣。學習與歡樂同行,自娛自樂。好了,此次避免你們的想法,我拿咱們的博客園實驗,我只是獲取前面三個頁面,太多了也是同樣的效果,沒有必要,說明方法可行就能夠了。ajax

開始代碼吧
一個解析類數組

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebBrowserCrawlerdemo
{
    //這種感受只適合單個頁面數據抓取//能夠抓取多個頁面如博客園的數據
    //http://www.cnblogs.com/rookey/p/5019090.html
    /// <summary>
    /// 經過WebBrowser抓取網頁數據
    /// WebBrowserCrawler  webBrowserCrawler=new WebBrowserCrawler();
    /// 示例:File.WriteAllText(Server.MapPath("sample.txt"),webBrowserCrawler.GetReult(http://www.in2.cc/sample/waterfalllab.htm));
    /// </summary>
    public  class WebBrowserCrawler
    {
        // WebBrowser
        private WebBrowser _WebBrowder;
        //最後結果
        private string _Result { get; set; }
        //網址
        private string _Path { get; set; }
        //當一直在抓取資料,容許等待的的最大秒數,超時時間(秒)
        private int _MaxWaitSeconds { get; set; }

        public delegate bool MyDelegate(object sender, TestEventArgs e);
        /// <summary>
        /// 是否達到中止加載條件
        /// </summary>
        public event MyDelegate IsStopEvent;

        /// <summary>
        /// 對外公開的Method
        /// </summary>
        /// <param name="url">URL Path</param>
        /// <param name="maxWaitSeconds">最大等待秒數</param>
        /// <returns></returns>
        public string GetReult(string url, int maxWaitSeconds = 60)
        {
            _Path = url;
            _MaxWaitSeconds = maxWaitSeconds <= 0 ? 60 : maxWaitSeconds;

            var mThread = new Thread(FatchDataToResult);
            //Apartment 是處理序當中讓物件共享相同執行緒存取需求的邏輯容器。 同一 Apartment 內的全部物件都能收到 Apartment 內任何執行緒所發出的
            //.NET Framework 並不使用 Apartment;Managed 物件必須自行以安全執行緒 (Thread-Safe) 的方式運用一切共
            //因為 COM 類別使用 Apartment,因此 Common Language Runtime 在 COM Interop 的狀況下呼叫出 COM 物件時必須創建 Apartment 並且加以初
            //Managed 執行緒能夠創建並且輸入只容許一個執行緒的單一執行緒 Apartment (STA),或者含有一個以上執行緒的多執行緒 Apartment (MT
            //只要把執行緒的 ApartmentState 屬性設定為其中一個 ApartmentState 列舉型別 (Enumeration),便可控制所創建的 Apartment 屬於哪種
            //因為特定執行緒一次只能初始化一個 COM Apartment,因此第一次呼叫 Unmanaged 程式碼之後就無法再變更 Apartment
            //From : http://msdn.microsoft.com/zh-tw/library/system.threading.apartmentstate.
            mThread.SetApartmentState(ApartmentState.STA);
            mThread.Start();
            mThread.Join();

            return _Result;
        }

        /// <summary>
        /// Call _WebBrowder 抓取資料
        /// For thread Call
        /// </summary>
        private void FatchDataToResult()
        {
            _WebBrowder = new WebBrowser();
            _WebBrowder.ScriptErrorsSuppressed = true;
            _WebBrowder.Navigate(_Path);
            DateTime firstTime = DateTime.Now;
            //處理目前在訊息佇列中的全部 Windows
            //若是在程式碼中呼叫 DoEvents,您的應用程式就能夠處理其餘事件。例如,若是您的表單將資料加入 ListBox 並將 DoEvents 加入程式碼中,則當另外一個視窗拖到您的表單上時,該表單將重
            //若是您從程式碼移除 DoEvents,您的表單將不會從新繪製,直到按鈕按一下的事件處理常式執
            //經過不斷循環把整個頁面都加載完,而後從中獲取本身想要的信息。能夠結合這個JumonyParser一塊兒用
            while ((DateTime.Now - firstTime).TotalSeconds <= _MaxWaitSeconds)
            {
                if (_WebBrowder.Document != null && _WebBrowder.Document.Body != null &&
                   !string.IsNullOrEmpty(_WebBrowder.Document.Body.OuterHtml) &&
                   this.IsStopEvent != null)
                {
                    string html = _WebBrowder.Document.Body.OuterHtml;
                    bool rs = this.IsStopEvent(null, new TestEventArgs(html));
                    if (rs)
                    {
                        this._Result = html;
                        break;
                    }
                }
                Application.DoEvents();
            }
            _WebBrowder.Dispose();
        }
    }
}

事件類
瀏覽器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebBrowserCrawlerdemo
{
   public class TestEventArgs:EventArgs
    {
       public string Html { get; set; }
       public TestEventArgs(string html2) {
           this.Html = html2;
       }
    }
}

 

調用端 先來一個界面吧安全

代碼post

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebBrowserCrawlerdemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public void test(int num)
        {
            WebBrowserCrawler obj = new WebBrowserCrawler();
            obj.IsStopEvent += new WebBrowserCrawler.MyDelegate((sender, e) =>
            {
                //當前html中已經加載了我想要的數據,返回true// 
           //return e.Html.Contains("<div id=\"post_list\">");
                return e.Html.Contains("<div class=\"post_item\">");
            });
       
            string url = string.Format("http://www.cnblogs.com/#p{0}", num);
            string html = obj.GetReult(url); //獲取採集的數據
            if (!string.IsNullOrEmpty(html))
            {
                //處理數據
                Write(html);
            }
        }

        private void btntest_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 4; i++)
            {
                test(i);
            }
        }
        //http://www.cnblogs.com/akwwl/p/3240813.html
        public void Write( string html)
        {
            string path = @"D:\練習\MyPictureDownloader\WebBrowserCrawlerdemo\bin\Debug\test\test.txt";
            FileStream fs = new FileStream(path, FileMode.Append);
            //得到字節數組
            byte[] data = System.Text.Encoding.Default.GetBytes(html);
            //開始寫入
            fs.Write(data, 0, data.Length);
            //清空緩衝區、關閉流
            fs.Flush();
            fs.Close();
        }
    }
}

說明一下,我數據是保存到TXT文件裏,沒有去分析什麼目標數據了,只要整個頁面獲取就能夠了,我是經過追加的形式保存的。學習

 

e.Html.Contains("<div id=\"post_list\">"); 分析爲啥不是這個,我用它結果獲取不到數據。原來是這樣的。

 

 

 

返回的是html元素格式,經過它,請求都尚未結束,沒有獲取到數據,確定不行了。因而改爲上面那個了。能夠獲取數據。
結果如圖,我只有獲取三頁所以三個<body>標籤,我也檢驗對比了,事實就是三頁的數據。

 

若是你還想獲取目標數據,能夠藉助一些HTML分析類如: Jumony,HtmlAgilityPack。好了,已經下班了。內容也介紹完了。參考:http://www.cnblogs.com/rookey/p/5019090.htmlhttp://www.cnblogs.com/akwwl/p/3240813.html
相關文章
相關標籤/搜索