0

HTML是:

<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>

我需要时间值,即 08:07:07,我将其转换为时间。我正在使用此代码获取 Div 区域的字符串。用于获取子字符串。为什么这不起作用?

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"));

                    }
                }
4

1 回答 1

0

已配置 HTML 敏捷包。现在将是什么代码。

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);
于 2013-04-28T18:10:05.480 回答