C#我可以抓取webBrowser控件中的链接吗

本文关键字:链接 控件 我可以 抓取 webBrowser | 更新日期: 2023-09-27 18:21:28

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

我有一个程序可以在网络浏览器控件中抓取网页以获取信息。

到目前为止,我可以获得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;

但是,表单上的Rich文本框填充了这个

System.Windows.Forms.HtmlElementCollection

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

谢谢克里斯。

C#我可以抓取webBrowser控件中的链接吗

有了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";
}