C#でjavascriptで生成されたWebページをスクレイピングする

元ネタはこれ。
stackoverflow.com
C# javascript スクレイピング」で検索すると引っかかるんだけどだいぶ前なので使えなくなってる。
 SeleniumとPhantomJSを使えって書いてあるが、PhantomJSは開発が終了してしまったらしく、Seleniumの方からもPhantomJSDriverが削除されてしまっている。
今後はChromeのヘッドレス(画面なし)モードを使えってことらしい。

Selenium + Chrome

ここ見たらインストールから使い方までわかる。
qiita.com
リンク先のはプロンプトを出さない処理がないので、プロンプトが出てくる。
出したくなかったら以下のリンク先のコードを使う。(PhantomJS用だけどほとんど同じ)
stackoverflow.com
参考に自分が書いたテストコード。

private static void TestChrome(string url)
{
    try
    {
        // プロンプトを出さないようにする
        using (var driverService = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService())
        {
            driverService.HideCommandPromptWindow = true;

            // 起動オプションの設定
            var options = new OpenQA.Selenium.Chrome.ChromeOptions();
            // ヘッドレス(画面なし)
            options.AddArgument("--headless");

            // ドライバ起動
            using (var driver = new OpenQA.Selenium.Chrome.ChromeDriver(driverService, options))
            {
                // URLへ移動
                driver.Url = url;
                driver.Navigate();

                // HTMLを取得する
                var source = driver.PageSource;
                Trace.WriteLine("------");
                Trace.WriteLine(source);

                // タイトルを取得する
                var title = driver.Title;
                Trace.WriteLine("------");
                Trace.WriteLine(title);
            }
        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine("Exception: " + ex.Message + Environment.NewLine + ex.StackTrace);
    }
}

Selenium + PhantomJS

それでもPhantomJSが使いたいんじゃって人は3.14.0以前を使う。3.13.1なら使えた。
NuGetから以下をインストール。

  • Selenium.WebDriver 3.13.1
  • PhantomJS 2.1.1

テストコード。

private static void TestPhantomJS(string url)
{
    try
    {
        // プロンプトを出さないようにする
        using (var driverService = OpenQA.Selenium.PhantomJS.PhantomJSDriverService.CreateDefaultService())
        {
            driverService.HideCommandPromptWindow = true;

            // ドライバ起動
            using (var driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(driverService))
            {
                // URLへ移動
                driver.Url = url;
                driver.Navigate();

                // HTMLを取得する
                var source = driver.PageSource;
                Trace.WriteLine("------");
                Trace.WriteLine(source);

                // タイトルを取得する
                var title = driver.Title;
                Trace.WriteLine("------");
                Trace.WriteLine(title);
            }
        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine("Exception: " + ex.Message + Environment.NewLine + ex.StackTrace);
    }
}

PhantomJSなんで消えてしまったん?

Seleniumのリリースノート
https://github.com/SeleniumHQ/selenium/releases

Selenium 3.14.0
・Removing long-deprecated local PhantomJS support from .NET :: Jim Evans

消えた理由
https://github.com/biocore/emperor/issues/580

ElDeveloper commented on 15 Apr 2017
The core maintainer of phantomjs has announced that he'll stop supporting phantomjs in favor of an upcoming feature of chrome, see link:
https://groups.google.com/forum/m/#!topic/phantomjs/9aI5d-LDuNE

This should not be a blocker yet, but it's useful to know about this.

https://groups.google.com/forum/m/#!topic/phantomjs/9aI5d-LDuNE

Hi,

I want to make an announcement.

Headless Chrome is coming - https://www.chromestatus.com/features/5678767817097216
(https://news.ycombinator.com/item?id=14101233)

I think people will switch to it, eventually. Chrome is faster and more stable than PhantomJS. And it doesn't eat memory like crazy.

I don't see any future in developing PhantomJS. Developing PhantomJS 2 and 2.5 as a single developer is a bloody hell.
Even with recently released 2.5 Beta version with new and shiny QtWebKit, I can't physically support all 3 platforms at once (I even bought the Mac for that!). We have no support.
From now, I am stepping down as maintainer. If someone wants to continue - feel free to reach me.

I want to give credits to Ariya, James and Ivan! It was the pleasure to work with you. Cheers!
I also want to say thanks to all people who supported and tried to help us. Thank you!

With regards,
Vitaly.

chromeがヘッドレスに対応したからいらんでしょってことらしい。
そしてPhantomJSも開発は終了してしまったらしい 。
jser.info

余談

AngleSharpでJavaScript有効化すると取れるって見つけたが取れなかった。なんでじゃ。
qiita.com
他にWebBrowser使う方法も引っかかったけどそもそもUIにまともに表示できてないしめんどくさそうなのでやめた。