我有一个小问题。
我必须只获得发货日期" 2015年7月17日"从字符串。让我们说这是我的代码:
string result = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "not-annotated hover")
{
result = el.InnerText;
textBox2.Text = result;
}
现在这是输出:
答案 0 :(得分:1)
string result = "";
string date = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "not-annotated hover")
{
result = el.InnerText;
date = Regex.Match(result ,
String.Format(@"{0}\s(?<words>[\w\s]+)\s{1}", "Ship Date:", "Country:"),
RegexOptions.IgnoreCase).Groups["words"].Value;
textBox2.Text = date ;
}
答案 1 :(得分:0)
好像你的div是外部div,你需要只显示发货日期的div。这将是最安全/最简单的方法。
但是,如果您拥有的是大字符串,则可以按换行字符进行拆分,并从以Ship date
开头的行中获取日期:
string[] lines = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
string dateString = lines
.FirstOrDefault(l => l.Trim().StartsWith("Ship date", StringComparison.InvariantCultureIgnoreCase));
DateTime shipDate;
if (dateString != null)
{
string[] formats = new[] { "MMMM dd, yyyy" };
string datePart = dateString.Split(':').Last().Trim();
bool validShipDate = DateTime.TryParseExact(
datePart,
formats,
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None,
out shipDate);
if (validShipDate)
Console.WriteLine(shipDate);
}
答案 2 :(得分:0)
从您分享的输出文字
string result = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "not-annotated hover")
{
result = el.InnerText;
if (result.IndexOf("Ship Date") == 0) //Ship Date text is present in the string
{
//since the string format is Ship Date: July 17, 2015 -
//we can assume : as a delimiter and split the text
string[] splitText = result.Split(':');
string date = splitText[1].Trim(); //this will give the date portion alone
}
textBox2.Text = result;
}
希望这有帮助。
注意:只有在收到的HTML中的发货日期字符串的格式与Output示例中指定的格式相同时,此逻辑才有效 p>