HTML是:
<div class="topheader-left common-txt" style="color: #ffffff;vertical-align:top;padding-top:3px">
Sunday, April 28, 2013 | 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"));
}
}
答案 0 :(得分:0)
配置了HTML Agility Pack。现在的代码是什么。
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);