使用 C# 获取网页元素的内容

use*_*169 3 c# browser

有什么方法可以从 ac# 应用程序中获取元素的内容或浏览器中打开的网页的控件吗?

我试图获得窗口 ex,但在与它进行任何形式的交流之后我不知道如何使用它。我也试过这个代码:

using (var client = new WebClient())
{
    var contents = client.DownloadString("http://www.google.com");
    Console.WriteLine(contents);
}
Run Code Online (Sandbox Code Playgroud)

这段代码给了我很多我无法使用的数据。

Dar*_*rov 5

您可以使用 HTML 解析器,例如HTML Agility Pack从您下载的 HTML 中提取您感兴趣的信息:

using (var client = new WebClient())
{
    // Download the HTML
    string html = client.DownloadString("http://www.google.com");

    // Now feed it to HTML Agility Pack:
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(html);

    // Now you could query the DOM. For example you could extract
    // all href attributes from all anchors:
    foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
        HtmlAttribute href = link.Attributes["href"];
        if (href != null)
        {
            Console.WriteLine(href.Value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)