4

私は現在C#とその楽しみを学んでいますが、障害にぶつかっています。

Webブラウザコントロール内のWebページをスクレイプして情報を取得できるプログラムがあります。

これまでのところ、HTMLを取得できます

HtmlWindow window = webBrowser1.Document.Window;
string str = window.Document.Body.OuterHtml;
richTextBox1.Text = (str.ToString());   

そしてテキスト

HtmlWindow window = webBrowser1.Document.Window;
string str = window.Document.Body.OuterText;
richTextBox1.Text = (str.ToString());

私はこのようなリンクをこすって表示しようとしました

HtmlWindow window = webBrowser1.Document.Window;
string str = window.Document.Body.GetElementsByTagName("A").ToString();
richTextBox1.Text = str;

ただし、代わりに、フォームのリッチテキストボックスにこれが表示されます

System.Windows.Forms.HtmlElementCollection

現在のWebページからリンクのリストを取得してテキストボックスに表示する方法を知っていますか?

クリスに感謝します。

4

1 に答える 1

3

HtmlAgilityパックを使用すると、簡単です。

HtmlWindow window = webBrowser1.Document.Window;
string str = window.Document.Body.OuterHtml;

HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument();
HtmlDoc.LoadHtml(str);

HtmlAgilityPack.HtmlNodeCollection Nodes = HtmlDoc.DocumentNode.SelectNodes("//a");

foreach (HtmlAgilityPack.HtmlNode Node in Nodes)
{
    textBox1.Text += Node.OuterHtml + "\r\n";
}
于 2012-01-25T15:15:11.057 に答える