首页 文章

Windows应用商店中的HtmlAgilityPack

提问于
浏览
3

所以我在控制台应用程序中有一些工作测试代码,我正在转移到Windows应用商店应用程序 . 不,问题是,我刚刚复制了我在控制台应用程序中的HtmlAgilityPack代码,现在它不起作用 . 我确实有HtmlAgilityPack作为参考...

现在一些HtmlAgilityPack确实有效 . 什么是无效的
"using (var client = new WebClient())"刚刚通过错误"The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)"

并且下一部分不起作用的是“foreach(doc.DocumentNode.SelectNodes中的HtmlNode链接(”// a [@href]“))”“在selectnodes部分,错误”'HtmlAgilityPack.HtmlNode'不包含'SelectNodes'的定义,没有扩展方法'SelectNodes'接受类型为'HtmlAgilityPack.HtmlNode'的第一个参数'(你是否缺少using指令或汇编引用)“

现在N知道Html Agility Pack依赖.NET来实现XPATH . 而且WinRT不支持XPATH . 现在我的问题是,我将如何使用将在Windows应用商店中运行的内容完成相同的操作?

下面的代码执行以下操作 . 从http://www.dubstep.net/track/5436下载html页面,一旦找到#,就会遍历它寻找href . 它需要高于它的href并将其作为uri发送 .

我已经验证下面的代码可以在控制台应用程序中工作 .

using (var client = new WebClient())
        {
            // Download the HTML
            string html = client.DownloadString("http://www.dubstep.net/track/5436");

            // Now feed it to HTML Agility Pack:
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            int i = 0;
            // Now you could query the DOM. For example you could extract
            // all href attributes from all anchors:
            List<string> list = new List<string>();
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                HtmlAttribute href = link.Attributes["href"];
                if (href != null)
                {
                    list.Add(href.Value);
                    i++;
                    if (href.Value == "#")
                    {
                        int t = i - 2;
                        Uri test = new Uri(list[t]);
                        start(test);
                    }
                }
            }
        }

 public static void start(Uri t)
    {
        Uri remoteUri = new Uri("http://soundcloud.com/dubstep/spag-heddy-the-master-vip/download");
        string fileName1 = "t", myStringWebResource = null;

        // Create a new WebClient instance.
        using (WebClient myWebClient = new WebClient())
        {
            myWebClient.DownloadFileCompleted += DownloadCompleted;
            myWebClient.DownloadProgressChanged += myWebClient_DownloadProgressChanged;
            myWebClient.DownloadFileAsync(t, "file.mp3");
        }
    }

2 回答

  • 3

    您可以尝试将 WebClient 替换为 HtmlWeb 并使用HtmlAgilityPack的LINQ API而不是XPath,以使其在Windows应用商店应用中运行:

    //use HAP's HtmlWeb instead of WebClient
    var htmlweb = new HtmlWeb();
    // load HtmlDocument from web URL
    HtmlDocument doc = htmlweb.Load("http://www.dubstep.net/track/5436");
    
    
    int i = 0;
    List<string> list = new List<string>();
    
    //use LINQ API to select all `<a>` having `href` attribute
    var links = doc.DocumentNode
                   .DescendantsAndSelf("a")
                   .Where(o => o.GetAttributeValue("href", null) != null);
    foreach (HtmlNode link in links)
    {
        HtmlAttribute href = link.Attributes["href"];
        if (href != null)
        {
            list.Add(href.Value);
            i++;
            if (href.Value == "#")
            {
                int t = i - 2;
                Uri test = new Uri(list[t]);
                start(test);
            }
        }
    }
    
  • 1

    对于XPath,您可以使用以下链接查找适用于Windows Phone的XPath的实现(源代码) . 代码很容易转移到WinRT .

    注意:使用LINQ通常远远优于使用XPath . 有一种情况并非如此 - 如果您的XPath来自服务器 . 在这种情况下,您可以使用此类解决方案 .

    http://socialebola.wordpress.com/2011/07/06/xpath-support-for-the-html-agility-pack-on-windows-phone/

相关问题