开发者

How to concatenate two nodes when using the HTML Agility Pack in a ASP.NET web app?s

I am using the agility pack to do some screens scraping开发者_Python百科 and my code so far to get titles is:

foreach (HtmlNode title in root.SelectNodes("//html//body//div//div//div[3]//div//div//div//div[3]//ul//li[1]//h4"))
        {
            string titleString = "<div class=\"show\">" + title.InnerText + "</div>";
            shows.Add(titleString);
        }

Before the title I want a timestamp related to the title and it has the node

/html/body/div/div/div[3]/div/div/div/div[3]/ul/li[1]/ul/li/span

How can I get this value next to the title? So something like:

string titleString = "<div class=\"show\">" + time.InnerText + " - " + title.InnerText + "</div>";


Try to get the parent node first and then get both title and timestamp from the parent

        HtmlNodeCollection TvGuideCollection = doc.DocumentNode.SelectNodes(@"//ul[@class='results']//ul//li");
        List<string> shows = new List<string>();
        foreach (HtmlNode item in TvGuideCollection)
        {
            HtmlNode title = item.SelectSingleNode(".//a");
            HtmlNode time = item.SelectSingleNode(".//span[@class='stamp']");
            if (title != null && time != null)
            {
                string titleString = "<div class=\"show\">" + time.InnerText + " - " + title.InnerText + "</div>";
                shows.Add(titleString);
            }
        }

Updated to just get todays shows

            HtmlNode TvGuideToday = doc.DocumentNode.SelectSingleNode(@"//ul[@class='results']//ul");
            List<string> shows = new List<string>();
            foreach (HtmlNode item in TvGuideToday.SelectNodes(".//li")) 
            {
                HtmlNode title = item.SelectSingleNode(".//a");
                HtmlNode time = item.SelectSingleNode(".//span[@class='stamp']");
                if (title != null && time != null)
                {
                    string titleString = "<div class=\"show\">" + time.InnerText + " - " + title.InnerText + "</div>";
                    shows.Add(titleString);
                }
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜