【问题标题】:C# can I Scrape a webBrowser control for links?C# 我可以为链接抓取 webBrowser 控件吗?
【发布时间】:2012-02-18 17:56:36
【问题描述】:

我目前正在学习 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

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

谢谢 克里斯。

【问题讨论】:

    标签: c# richtextbox hyperlink scrape


    【解决方案1】:

    使用 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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 2023-04-09
      • 2018-01-04
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多