3

ac#アプリからブラウザで開いているWebページの要素またはコントロールのコンテンツを取得する方法はありますか?

私はウィンドウを元に戻そうとしましたが、それと何らかの通信を行うためにそれを使用する方法がわかりません。私もこのコードを試しました:

using (var client = new WebClient())
{
    var contents = client.DownloadString("http://www.google.com");
    Console.WriteLine(contents);
}

このコードは、私が使用できない多くのデータを提供します。

4

1 に答える 1

5

HTML Agility PackダウンロードしたHTMLから関心のある情報を抽出するなどのHTMLパーサーを使用できます。

using (var client = new WebClient())
{
    // Download the HTML
    string html = client.DownloadString("http://www.google.com");

    // Now feed it to HTML Agility Pack:
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(html);

    // Now you could query the DOM. For example you could extract
    // all href attributes from all anchors:
    foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
        HtmlAttribute href = link.Attributes["href"];
        if (href != null)
        {
            Console.WriteLine(href.Value);
        }
    }
}
于 2013-01-13T17:13:26.787 に答える