【问题标题】:HTML Parser for Windows 10 (UWP)适用于 Windows 10 (UWP) 的 HTML 解析器
【发布时间】:2015-07-16 12:38:58
【问题描述】:

是否有任何 HTML 解析器使用 X-Path 来处理 UWP - Windows 10?

在 Windows 10 中探索 HTML 的最简单/最佳方式是什么?

【问题讨论】:

  • @Graffito 不支持 ... SelectNode 方法出错 ... 像 Windows Phone 8.1
  • 您是否将 Html 文档声明为“HtmlAgilityPack.HtmlDocument”,而不仅仅是“HtmlDocument”?
  • 是的,我试过这段代码(new HtmlAgilityPack.HtmlDocument()).DocumentNode.SelectNodes但是没有SelectNodes方法
  • Official answer from the author - 你不能使用 XPath;您必须改用 LINQ。

标签: c# windows-10 windows-10-mobile


【解决方案1】:

解决方案 1:安装包 HtmlAgilityPack

https://www.nuget.org/packages/HtmlAgilityPack/

解决方案2:解析Html

private async void Parsing(string website)
       {
           try
           {
               HttpClient http = new HttpClient();
               var response = await http.GetByteArrayAsync(website);
               String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
               source = WebUtility.HtmlDecode(source);
               HtmlDocument resultat = new HtmlDocument();
               resultat.LoadHtml(source);


           List<HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
           (x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("block_content"))).ToList();

           var li = toftitle[6].Descendants("li").ToList();
           foreach (var item in li)
           {
               var link = item.Descendants("a").ToList()[0].GetAttributeValue("href", null);
               var img = item.Descendants("img").ToList()[0].GetAttributeValue("src", null);
               var title = item.Descendants("h5").ToList()[0].InnerText;

               listproduct.Add(new Product()
               {
                   Img = img,
                   Title = title,
                   Link = link
               });
           }

       }
       catch (Exception)
       {

           MessageBox.Show("Network Problem!");
       }

   }

Windows 8 Parsing Html using C# sample in C# for Visual Studio 2013[^]

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多