简体   繁体   中英

Getting String between Div with the help of Class Name

HTML is:

<div class="topheader-left common-txt" style="color: #ffffff;vertical-align:top;padding-top:3px">
            &nbsp;Sunday,&nbsp; April&nbsp;&nbsp;28,&nbsp;2013&nbsp;|&nbsp;08:07:07 PM
        </div>

I need the value of time which is 08:07:07 which i will convert into time. I am using this code to Get String of Div Area. for getting substring. Why this is not working?

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
                theElementCollection = webBrowser1.Document.GetElementsByTagName("div");
                foreach (HtmlElement curElement in theElementCollection)
                {
                   if (curElement.GetAttribute("class").ToString() == "topheader-left common-txt")
                    {
                        MessageBox.Show(curElement.GetAttribute("InnerText"));

                    }
                }

HTML Agility Pack configured. what will be the code now.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var innerText = doc.DocumentNode
                   .SelectSingleNode("//div[@class='topheader-left common-txt']")
                   .InnerText;


DateTime time = DateTime.ParseExact(WebUtility.HtmlDecode(innerText).Split('|')[1].Trim(), 
                                    "hh:mm:ss tt",
                                    CultureInfo.InvariantCulture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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