繁体   English   中英

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

[英]Get the content of an element of a Web page using C#

是否可以通过ac#app在浏览器中获取元素的内容或打开的网页的控件?

我试图获得window ex,但是在与它进行任何形式的通信之后我不知道如何使用它。 我也尝试了这段代码:

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

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

您可以使用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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM