我在运行一小段代码时收到一条错误消息,说输入字符串不是正确的格式。问题来自解析这些html
标记一个
<td class="tdRow1Color" width="100%">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td class="plaintextbold">Item Number: 1258</td></tr>
<tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr>
<tr>
<td class="plaintext" valign="middle"> <img src="../images/0note.gif" border="0" align="absmiddle"> <a class="prodlink" href="writeReview.asp?number=1258"><i><u>Be the first to review this item</u></i></a></td>
</tr>
<tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr>
<tr><td class="plaintext"><b>RRP £50.00 - Now £39.99</b> </td>
标记两个
<tr><td class="tdRow1Color" width="100%">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td class="plaintextbold">Item Number: 2525</td></tr>
<tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr>
<tr>
<td class="plaintext" valign="middle"> <img src="../images/0note.gif" border="0" align="absmiddle"> <a class="prodlink" href="writeReview.asp?number=2525"><i><u>Be the first to review this item</u></i></a></td>
</tr>
<tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr>
<tr><td class="plaintext">RRP £45 - Now £38
我正在通过此正则表达式转换RRP价格。
private Regex _originalPriceRegex = new Regex(@"RRP \s(\d+\.?\d+?)");
通过xpath获取RRP价格
ProductProperties.priceOriginal, new HtmlElementLocator("//td[@class='tdRow1Color']//td[@class='plaintext']//text()[starts-with(., 'RRP')]",
当xpath值传递到下面的函数时,似乎会出现问题。当它返回priceMatch.Groups [1] .Value
时抛出异常 private string LookForOrignalPrice(HtmlNode node)
{
string text = node.InnerText;
Match priceMatch = _originalPriceRegex.Match(text);
if (priceMatch.Success)
Console.WriteLine("++++++price is " + priceMatch);
return priceMatch.Groups[1].Value;
return null;
}
感谢您提供任何建议。
答案 0 :(得分:4)
当使用它的良好做法来放置大括号时,否则你可能会遇到这样的错误。
priceMatch.Groups[1].Value
为false时执行priceMatch.Success
。
所以你应该改变你的代码:
private string LookForOrignalPrice(HtmlNode node)
{
string text = node.InnerText;
Match priceMatch = _originalPriceRegex.Match(text);
if (priceMatch.Success) --> put braces to group the statements
{
Console.WriteLine("++++++price is " + priceMatch);
return priceMatch.Groups[1].Value;
}
return null;
}