爬去网页内容--错误提示:并非所有的代码都返回值。

www61550125 2012-03-30 07:29:54

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


...全文
112 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zj25810 2012-04-02
  • 打赏
  • 举报
回复
你必须要判断每一个条件,每一种情况都要有返回值。
paypay2012 2012-04-02
  • 打赏
  • 举报
回复


public GuoJiPrice GetGuoJiPriceInfo(string url)
{
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;
}
}
cykb518 2012-03-31
  • 打赏
  • 举报
回复
在 foreach 的时候 result 应该new 一下,在用之前
result= new GuoJiPrice();
cykb518 2012-03-31
  • 打赏
  • 举报
回复

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


哎 有时候自己多思考思考吧
落阳 2012-03-31
  • 打赏
  • 举报
回复
建议,循环里只进行赋值,方法最后,返回。
你现在的代码,循环进行1次就retrun了。并且若不执行循环,也没有“返回”,应该补上。

这样就不会报错了。
hebeijg 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
C# code


public GuoJiPrice GetGuoJiPriceInfo(string url)
{
try
{
string now = DateTime.Now.ToString("yyyy-MM-dd");
url = string……
[/Quote]

说句心里话,你写的这个虽然说可以,但是真不能这么写的,
怎么可以把返回值放到循环里
www61550125 2012-03-30
  • 打赏
  • 举报
回复
应该怎么弄?
www61550125 2012-03-30
  • 打赏
  • 举报
回复
测试了一下,发现还是只能获取到整个表格的第一行的数据。 不能循环。

我之前的代码:

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);
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行的数据?
porschev 2012-03-30
  • 打赏
  • 举报
回复



...
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);
...
相当之稳重 2012-03-30
  • 打赏
  • 举报
回复
你这个函数是要有返回值的,但是你的程序流程不是每个分支都有返回值。
cykb518 2012-03-30
  • 打赏
  • 举报
回复
不确定你是要一个对象还是一个对象集合回来,如果你只返回一个集合 可以向下面这样改一下.但是感觉你应该需要返回一个集合。

public GuoJiPrice GetGuoJiPriceInfo(string url)
{
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;
}
threenewbee 2012-03-30
  • 打赏
  • 举报
回复
return new GuoJiPrice(a, b, c, d, now);
这是不行的。当matches为空,返回什么呢?

必须有一个写在foreach之外。
mngzilin 2012-03-30
  • 打赏
  • 举报
回复
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return null;
------------------
加上一句

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧