LoginSignup
0
2

More than 5 years have passed since last update.

C#で任意のサイトの情報を抜き取る - ウェブスクレイピングはじめの一歩

Last updated at Posted at 2016-12-22

やりたいこと

  • 任意のサイトから特定の情報を抜き出したい

[以下、2016/12/23追記]


ただし、こちらの記事Jsoupで簡単ウェブスクレイピングで次のように注意している通り、気をつけなくてはいけませんよね。

注意事項:スクレイピングの可否は先方の規約に依存します。例えば、Amazonのようにスクレイピングを禁止しているサイトへのスクレイピングは禁止です。場合によっては法的な措置を取られますのでルールはちゃんと守りましょう。

すみません、自分はこちらを読むまで意識が低かったです。
あわてて調べてみましたが、一応それらしい情報が見つけられなかったので、進めていきます。


背景

YouTubeやニコニコ動画での投稿をはじめましたが、ニコニコ動画での動画情報の編集が気に入らない。また、将来的にYouTubeやニコニコ動画の動画説明などを同期したり、テンプレートを活用するなどしたい

実装

ソースコード全体です。

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

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

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.nicovideo.jp/my/video");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            System.Diagnostics.Debug.Print(e.Url.ToString());

            if (e.Url.Equals("http://www.nicovideo.jp/my/video"))
            {
                HtmlElement el = webBrowser1.Document.GetElementById("video").Children[5];
                for (int i = 1; i < el.Children.Count; i++)
                {
                    HtmlElement elm = el.Children[i].Children[1].Children[0].Children[0];
                    if (elm != null)
                    {
                        string key = elm.GetAttribute("href").Replace("http://www.nicovideo.jp/watch/", "");
                        treeView1.Nodes.Add(key, elm.InnerText).Tag = key.Replace("sm", "");
                    }
                }
            }
            else if (0 <= e.Url.ToString().IndexOf("http://www.upload.nicovideo.jp/edit?f="))
            {
                textBox1.Text = webBrowser1.Document.GetElementById("movie_title").GetAttribute("value");
                textBox2.Text = webBrowser1.Document.GetElementById("movie_detail").OuterText;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            webBrowser1.Navigate($"http://www.upload.nicovideo.jp/edit?f={e.Node.Tag}");
        }
    }
}

元となっている記事

関連記事

以上

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2