解析一个特定的div HtmlAgilityPack

时间:2014-03-30 12:23:44

标签: c# parsing windows-phone-8

我正在尝试使用WP8的HtmlAgilityPack来解析这个htmlpage:http://mp3skull.com/mp3/eminem.html。 我必须使用这种风格的所有div:“font-size:15px;”。 我写了这个:

HttpWebRequest httpRequest = (HttpWebRequest)result.AsyncState;
                    WebResponse response = httpRequest.EndGetResponse(result);

                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream);
                    strResponse = reader.ReadToEnd();

                    HtmlDocument htmlDocument = new HtmlDocument();
                    htmlDocument.OptionFixNestedTags = true;
                    htmlDocument.LoadHtml(strResponse);

                    if (htmlDocument.DocumentNode != null)
                    {
                        // parsing page's title
                        HtmlAgilityPack.HtmlNode titleNode = htmlDocument.DocumentNode.SelectSingleNode("//title");
                        if (titleNode != null)
                        {
                            Vista.Title = titleNode.InnerText;
                        }

                        var elements = htmlDocument.DocumentNode.SelectNodes("//div['style=font-size:15px;']");

                        if (elements != null)
                        {
                            for (int i = 0; i < elements.Count; i++)
                            {
                                risultati.Add(elements[i].InnerHtml.Trim());
                            }
                            //LLSResult.ItemsSource = risultati;
                            test.Text = risultati.ElementAt(0).ToString();
                        }
                    }

打印标题,但“risultati”(ObservableCollection)中的元素不打印。 此外,应用程序在完成工作几秒后关闭。

由于

1 个答案:

答案 0 :(得分:0)

如果您只是想找到解析标题的方法。这是我的尝试:

class Program
{
    static void Main(string[] args)
    {

        using (var webClient = new WebClient())
        {
             webClient.DownloadStringCompleted+=webClient_DownloadStringCompleted;
             webClient.DownloadStringAsync(new Uri("http://mp3skull.com/mp3/eminem.html"));          
        }

        System.Diagnostics.Process.GetCurrentProcess().WaitForExit();

    }

    static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Console.WriteLine("Error: {0}", e.Error.Message);
            return;
        }

        var source = e.Result.Trim();

        if (string.IsNullOrEmpty(source))
        {
            Console.WriteLine("Page not returned.");
            return;
        }


        foreach (Match match in Regex.Matches(source,"<div style=\"font-size:15px;\"><b>(?<title>.*?)</b></div>"))
        {
            Console.WriteLine(match.Groups["title"].Value);
        }

    }
}