【问题标题】:How can i extract links from string with html content using htmlagilitypack?如何使用 htmlagilitypack 从带有 html 内容的字符串中提取链接?
【发布时间】:2015-10-04 21:31:50
【问题描述】:
for (int i = 0; i < numberoflinks; i++)
{
    string downloadString = client.DownloadString(mainlink+i+".html");
    var document = new HtmlWeb().Load(url);
    var urls = document.DocumentNode.Descendants("img")
                        .Select(e => e.GetAttributeValue("src", null))
                        .Where(s => !String.IsNullOrEmpty(s))
}      

问题是 HtmlWeb().Load 需要一个 html url,但我想加载其中已经包含 html 内容的字符串 downloadString。

更新:

我现在试过了:

for (int i = 0; i < numberoflinks; i++)
            {

                string downloadString = client.DownloadString(mainlink+i+".html");
                HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                document.Load(downloadString);
                var urls = document.DocumentNode.Descendants("img")
                                                .Select(e => e.GetAttributeValue("src", null))
                                                .Where(s => !String.IsNullOrEmpty(s));
            }

但是我遇到了异常:

document.Load(downloadString);

路径中有非法字符

我要做的是从每个链接下载/提取所有 .JPG 图像。 无需先将 url 下载到硬盘,而是将内容下载到字符串中提取此 html 中所有以 .JPG 结尾的图像链接,然后下载 JPG。

【问题讨论】:

    标签: c# .net winforms html-agility-pack


    【解决方案1】:

    您应该能够使用HtmlDocumentLoadHtml() 方法处理HTML 字符串。

    来自源代码:

    public void LoadHtml(string html)

    从指定的字符串加载 HTML 文档。

    param name="html"

    包含要加载的 HTML 文档的字符串。不能为空。

    Load 方法需要一个文件名,这就是关于 illegal characters in path 的消息的原因。

    【讨论】:

      猜你喜欢
      • 2014-09-25
      • 2011-12-01
      • 2014-08-15
      • 2021-09-24
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      相关资源
      最近更新 更多