c# - 如何使用 HTML Agility Pack 从网站检索所有图像?

标签 c# parsing html-agility-pack

我刚刚下载了 HTMLAgilityPack,文档中没有任何示例。

我正在寻找一种从网站下载所有图像的方法。地址字符串,而不是物理图像。

<img src="blabalbalbal.jpeg" />

我需要提取每个 img 标签的来源。我只是想感受一下图书馆及其可以提供的服务。每个人都说这是完成这项工作的最佳工具。

编辑

public void GetAllImages()
    {
        WebClient x = new WebClient();
        string source = x.DownloadString(@"http://www.google.com");

        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
        document.Load(source);

                         //I can't use the Descendants method. It doesn't appear.
        var ImageURLS = document.desc
                   .Select(e => e.GetAttributeValue("src", null))
                   .Where(s => !String.IsNullOrEmpty(s));        
    }

最佳答案

您可以使用 LINQ 执行此操作,如下所示:

var document = new HtmlWeb().Load(url);
var urls = document.DocumentNode.Descendants("img")
                                .Select(e => e.GetAttributeValue("src", null))
                                .Where(s => !String.IsNullOrEmpty(s));

编辑:这段代码现在确实有效;我忘了写 document.DocumentNode

关于c# - 如何使用 HTML Agility Pack 从网站检索所有图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2113924/

相关文章:

c++ - 如何在 C++ 中解析具有不同字段数的行

python - 如何从 Coinmarketcap 解析 BTC 历史数据?

python - 在 python 中解析法语日期

javascript - Html 节点内的信息不可见

c# - 如何保存动态复选框更改

c# - 无法在 Visual Studio 2012 中编译 TypeScript 文件

c# - 如何阻止用户打开 Internet Explorer 或 Firefox 等新进程?

html-parsing - 为什么我不能在 windows phone 8 上使用 htmlagilitypack?我还能用什么来解析 WP8 中的 HTML?

C# HtmlAgilityPack 从特定的 h2 中选择表格

c# - 限制子类中的访问属性