.NET

スクレイピングって面白いわよね。わたしは Ruby でよくやるのだけど、.NET でもできるわ。

たぶん VS2010ではダメ。VS2013以降ならイケると思うの。HtmlAgilityPack を使うのがいいわ。

HtmlAgilityPack の導入(VS2013)

書いてみましょうか。

ダウンロードはめんどくさいから、webBrowserで落とす事にしたわ。そこいらは手抜きね。

サンプルサイトは、ここ。2016年の祝日をもらってきましょう。

namespace web002 {

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

       private void button1_Click(object sender, EventArgs e)
       {
           textBox1.Text = "http://calendar.infocharge.net/cal/2016/";
           if (textBox1.Text != "")
           {
               button1.Enabled = false;
               webBrowser1.Url = new Uri(textBox1.Text);
           }
       }

       private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
       {
           button1.Enabled = true;
           HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
           html.LoadHtml(webBrowser1.DocumentText);
           var articles = html.DocumentNode.SelectNodes(@"//tr/td")
               .Select(a=>new{
                   Html = a.InnerHtml
               });
           textBox2.Text = "";
           List<string> ss = new List<string>();
           foreach (var a in articles)
           {
               string kg = "\r\n";
               if (Regex.IsMatch(a.Html, @"^[0-9]", RegexOptions.IgnoreCase) == true)
               {
                   kg = "\t";
               }
               string x1 = a.Html + kg;
               textBox2.Text += x1;
           }
       }
   }

}

さすが簡単ねえ。

ちなみに単発データの取得だと、以下かしら?

HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode(xpath);

ここのデータを使わせてもらいました。

日本の祝日カレンダー(2016)
http://calendar.infocharge.net/cal/2016/

トップ   編集 凍結解除 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-04-20 (水) 19:22:40