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


     前一篇文章寫到我獲取數據的方式不是通過分析HTML獲得,而是通過分析請求鏈接,然后模擬請求方法獲取數據,這只是一種方法。而且是在我通過分析HTML獲取不到的情況下,曲線救國,參考別人文章實現的。很高興,我實現了自己獲取數據的目標。我以為這樣就算結束了。可是,今天又發現了另外一種方法,而且是通過分析HTML實現的,看到它,我感覺太不可思議了,我花了那么多的時間都沒有實現,怎么現在又可以了。現在興趣正濃,趕緊操刀實踐一番。於是有了這篇,算是意外之喜吧!

 

    先說明一下實現思路,原來它是通過調用WebBrowser控件來實現的。怪不得它可以獲取HTML,然后分析獲取數據。管你什么動態解析,ajax,現在我是瀏覽器行為了,所有的都逃不過我的法眼。真的是不錯的選擇方式。

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


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

開始代碼吧
一個解析類

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

 

調用端 先來一個界面吧

代碼

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.html
http://www.cnblogs.com/akwwl/p/3240813.html

 


免責聲明!

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



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