public GuoJiPrice GetGuoJiPriceInfo(string url)
{
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string.Format("{0}_{1}_{1}_1.htm", url, now);
string form = Http.GetHtml(url, ref cookie);//获取页面
form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
foreach (Match m in matches)
{
string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //获取第一个匹配到的内容
string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
return new GuoJiPrice(a, b, c, d, now);
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
{
Console.WriteLine(ex.Message);
return null;
}
return null;
------------------
加上一句
这是不行的。当matches为空,返回什么呢?必须有一个写在foreach之外。
{
GuoJiPrice result=null;
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string.Format("{0}_{1}_{1}_1.htm", url, now);
string form = Http.GetHtml(url, ref cookie);//获取页面
form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
foreach (Match m in matches)
{
string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //获取第一个匹配到的内容
string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
result= GuoJiPrice(a, b, c, d, now);
break;
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
result=null;
}
retrun result;
}
...
string a = string.Empty;
string b = string.Empty;
string c = string.Empty;
string d = string.Empty;
foreach (Match m in matches)
{
a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //获取第一个匹配到的内容
b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
}
return new GuoJiPrice(a, b, c, d, now);
...
{
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string.Format("{0}_{1}_{1}_1.htm", url, now);
string form = Http.GetHtml(url, ref cookie);
form = Other.GetRegValue("<tr><td((?!</tr>).)+", form); MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);
string a = Other.GetRegValue(@".*", matches[1].Value);
string b = Other.GetRegValue(@".*", matches[2].Value);
string c = Other.GetRegValue(@".*", matches[3].Value);
string d = Other.GetRegValue(@".*", matches[4].Value);
return new GuoJiPrice(a, b, c, d, now); }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}这样我能正常获取到这张表格的第一行,匹配到的4个单元格的数据。但是:假如这个表格有10行的时候,我只能读取到第一行的数据,我怎样才能读取到这整10行的数据?
你现在的代码,循环进行1次就retrun了。并且若不执行循环,也没有“返回”,应该补上。这样就不会报错了。
public List<GuoJiPrice> GetGuoJiPriceInfo(string url)
{
List<GuoJiPrice> alllist=new List<GuoJiPrice>();
GuoJiPrice result=null;
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string.Format("{0}_{1}_{1}_1.htm", url, now);
string form = Http.GetHtml(url, ref cookie);//获取页面
form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
foreach (Match m in matches)
{
string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //获取第一个匹配到的内容
string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'> ", ""));].Value; //
result= GuoJiPrice(a, b, c, d, now);
alllist.add(result);
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
retrun alllist;
}
哎 有时候自己多思考思考吧
result= new GuoJiPrice();
{
GuoJiPrice result = null;
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string.Format("{0}_{1}_{1}_1.htm", url, now);
string form = Http.GetHtml(url, ref cookie);
string form2 = Other.GetRegValue("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='ProTab marginT'><tr((?!</table>).)+",form);
MatchCollection trmatches = Other.GetRegValues("<tr><td((?!</tr>).)+", form2);
foreach (Match match in trmatches)
{
MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", match.Value);
string a = Other.GetRegValue(@".*", matches[1].Value.Replace("<td align='center'> ", ""));
string b = Other.GetRegValue(@".*", matches[2].Value.Replace("<td align='center'> ", ""));
string c = Other.GetRegValue(@".*", matches[3].Value.Replace("<td align='center'> ", ""));
string d = Other.GetRegValue(@".*", matches[4].Value.Replace("<td><span class='down'>", " "));
MessageBox.Show(a);//这里获取到了2次数据,循环。
result = new GuoJiPrice(a, b, c, d, now); }
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return result;
}返回写入数据库方法的时候,只获取到了一次数据,默认第一条数据public static bool AddGuoJiPrice(int Id, int ClassId, GuoJiPrice guojiprices)//国际市场价格写入
{
try
{
MessageBox.Show(guojiprices.ZhongLiang);//这里只取到一次数据,也就是说跟没循环一样
string sql = string.Format("insert into GuoJiJiaGe(ClassId,SmallId,Addtime,ZhongLiang,JiaGe,ChanDi,ZhangDie) values({1},{0},#{2}#)", ClassId, Id, guojiprices.AddTime);
OleDbCommand cmd = new OleDbCommand(sql, con);
return cmd.ExecuteNonQuery() > 0;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}