[英]C# can I Scrape a webBrowser control for links?
到目前为止,我正在学习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;
但相反,表单上的Rich文本框会填充此内容
System.Windows.Forms.HtmlElementCollection
你知道如何从当前网页获取一个链接列表,以便在文本框中显示吗?
谢谢克里斯。
使用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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.