简体   繁体   English

使用Windows Phone C从HTML页面获取数据

[英]get data from html page with windows phone c#

I have this page: www.unnu.com/music-artists 我有此页面:www.unnu.com/music-artists

need to retrieve this complete list of all the artists and their links. 需要检索所有艺术家及其链接的完整列表。

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

            // There are various options, set as needed
            htmlDoc.OptionFixNestedTags = true;

            // filePath is a path to a file containing the html
            htmlDoc.Load("http://www.unnu.com/music-artists");

            // Use:  htmlDoc.LoadHtml(xmlString);  to load from a string (was htmlDoc.LoadXML(xmlString)

            // ParseErrors is an ArrayList containing any errors from the Load statement
            if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
            {
                // Handle any parse errors as required

            }
            else
            {

                if (htmlDoc.DocumentNode != null)
                {
                    HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//a");

                    if (bodyNode != null)
                    {
                        listBox1 = ????//this is not working, do not know if the code is correct and just missing here or is this all wrong.
                    }
                }
            }

Please, I need this with urgency as it is a job. 拜托,这是我的工作,我急需此。 If you need my skype is gnrmalazagnr can add. 如果需要我的Skype,可以添加gnrmalazagnr。

I am not sure what you want to do here, but if just want to download and read the html file. 我不确定您要在这里做什么,但是如果只是想下载并阅读html文件。 you need WebClient web = new WebClient(); web.DownloadStringAsync(new Uri("www.unnu.com/music-artists")); web.DownloadStringCompleted += web_DownloadStringCompleted; 您需要WebClient web = new WebClient(); web.DownloadStringAsync(new Uri("www.unnu.com/music-artists")); web.DownloadStringCompleted += web_DownloadStringCompleted; WebClient web = new WebClient(); web.DownloadStringAsync(new Uri("www.unnu.com/music-artists")); web.DownloadStringCompleted += web_DownloadStringCompleted;

void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { string HtmlPage = e.Result; }

I resolve the problem thanks: 我解决了问题,谢谢:

void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { void web_DownloadStringCompleted(对象发送者,Dow​​nloadStringCompletedEventArgs e){

            if (!e.Cancelled && e.Error == null && !String.IsNullOrEmpty(e.Result))
            {
                _artistas = new List<Artista>();
                // Aqui você pega todos os links da página
                // P.S.: Se a página mudar, você tem que alterar o pattern aqui.
                string pattern = @"\<a\shref\=[\""|\'](?<url>[^\""|\']+)[\""|\']\stitle\=[\""|\'](?<title>[^\""|\']+)[\""|\']\>(?<author>[^\<]+)\<\/a\>";
                // Busca no HTML todos os links
                MatchCollection ms = Regex.Matches(e.Result, pattern, RegexOptions.Multiline);

                Debug.WriteLine("----- OK {0} links encontrados", ms.Count);

                foreach (Match m in ms)
                {
                    // O pattern acima está dizendo onde fica o Url e onde fica o nome do artista
                    // e esses são resgatados aqui
                    Group url = m.Groups["url"];
                    Group author = m.Groups["author"];

                    if (url != null && author != null)
                    {
                        //Debug.WriteLine("author: {0}\nUrl: {1}", author.Value, url.Value);

                        // Se caso tenha encontrado o link do artista (pois há outros links na página) continua
                        if(url.Value.ToLower().IndexOf("/artist/") > -1)
                        {
                            // Adiciona um objeto Artista à lista
                            Artista artista = new Artista(author.Value, url.Value);
                            _artistas.Add(artista);
                        }
                    }
                }

                listBox1.ItemsSource = _artistas;

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM