【问题标题】:How do i extract all http links from html file using htamlagilitypack and then extract http links between tags?如何使用 htamlagilitypack 从 html 文件中提取所有 http 链接,然后在标签之间提取 http 链接?
【发布时间】:2013-11-24 21:07:20
【问题描述】:

我正在尝试这段代码:

private void htmlparsing(string htmlfile)
        {
            List<string> test = new List<string>();
            HtmlDocument doc = new HtmlDocument();
            doc.Load(htmlfile);
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                test.Add(att.Value);  
            }
            doc.Save(@"d:\file.htm");
        }

这是我正在处理的 html 文件:https://skydrive.live.com/redir?resid=EB1C71C44C3976D5!318&authkey=!AKxxwSboig3BQpo

当我使用断点并在工作完成后观看列表测试时,我看到 154 个链接,但我没有看到例如 html 文件内容中的链接:

"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.cld&datum=201311151500&cultuur=en-GB&continent=europa","http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.cld&datum=201311151800&cultuur=en-GB&continent=europa"

有很多链接有61-62个链接,我在列表测试中没有看到这个链接。

第二个链接在:

var images = new Array(

最后

);

所以第一步我想从 html 文件中获取所有 http 链接。 其次,我想过滤并从 html 文件中获取所有 http 链接: var images = new Array( 和 );

【问题讨论】:

  • 隔离问题。仅使用相关的 HTML 对其进行测试,将 URL 替换为 http://example.com 并在此处显示 HTML。

标签: c# html-agility-pack


【解决方案1】:

希望这对您有用。此代码仅适用于此 var images = new Array()

中的链接
            List<string> test = new List<string>();
            string extractUrls = YourHtmlInText;
            extractUrls = extractUrls.Remove(0, extractUrls.IndexOf("var images = new Array(") + " var images = new Array(".Length);
            extractUrls = extractUrls.Substring(0, extractUrls.IndexOf(";")).Replace(")", "").Trim();
            string[] urls = extractUrls.Split(',');
            foreach (String url in urls)
            {
                test.Add(url.Trim().Replace("\"",""));
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多