4

到目前为止,我正在学习 C# 和它的乐趣,但我遇到了障碍。

我有一个程序可以在 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

您知道如何从当前网页获取链接列表以显示在文本框中吗?

谢谢克里斯。

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 回答