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

问题描述

publicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//returnnewGuoJiPrice(a,b,c,d,now);}}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}}

解决方案

解决方案二:
catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}returnnull;------------------加上一句
解决方案三:
returnnewGuoJiPrice(a,b,c,d,now);这是不行的。当matches为空,返回什么呢?必须有一个写在foreach之外。
解决方案四:
不确定你是要一个对象还是一个对象集合回来,如果你只返回一个集合可以向下面这样改一下.但是感觉你应该需要返回一个集合。publicGuoJiPriceGetGuoJiPriceInfo(stringurl){GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//result=GuoJiPrice(a,b,c,d,now);break;}}catch(Exceptionex){Console.WriteLine(ex.Message);result=null;}retrunresult;}

解决方案五:
你这个函数是要有返回值的,但是你的程序流程不是每个分支都有返回值。
解决方案六:
...stringa=string.Empty;stringb=string.Empty;stringc=string.Empty;stringd=string.Empty;foreach(Matchminmatches){a=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//获取第一个匹配到的内容b=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//c=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//d=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//}returnnewGuoJiPrice(a,b,c,d,now);...

解决方案七:
测试了一下,发现还是只能获取到整个表格的第一行的数据。不能循环。我之前的代码:publicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);stringa=Other.GetRegValue(@".*",matches[1].Value);stringb=Other.GetRegValue(@".*",matches[2].Value);stringc=Other.GetRegValue(@".*",matches[3].Value);stringd=Other.GetRegValue(@".*",matches[4].Value);returnnewGuoJiPrice(a,b,c,d,now);}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}}

这样我能正常获取到这张表格的第一行,匹配到的4个单元格的数据。但是:假如这个表格有10行的时候,我只能读取到第一行的数据,我怎样才能读取到这整10行的数据?
解决方案八:
应该怎么弄?
解决方案九:
引用楼主的回复:

C#codepublicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string……

说句心里话,你写的这个虽然说可以,但是真不能这么写的,怎么可以把返回值放到循环里
解决方案十:
建议,循环里只进行赋值,方法最后,返回。你现在的代码,循环进行1次就retrun了。并且若不执行循环,也没有“返回”,应该补上。这样就不会报错了。
解决方案十一:
publicList<GuoJiPrice>GetGuoJiPriceInfo(stringurl){List<GuoJiPrice>alllist=newList<GuoJiPrice>();GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));].Value;//result=GuoJiPrice(a,b,c,d,now);alllist.add(result);}}catch(Exceptionex){Console.WriteLine(ex.Message);}retrunalllist;}

哎有时候自己多思考思考吧
解决方案十二:
在foreach的时候result应该new一下,在用之前result=newGuoJiPrice();
解决方案十三:
该回复于2012-04-01 10:25:44被版主删除
解决方案十四:
publicGuoJiPriceGetGuoJiPriceInfo(stringurl){GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);stringform2=Other.GetRegValue("<tablewidth='100%'border='0'cellspacing='0'cellpadding='0'class='ProTabmarginT'><tr((?!</table>).)+",form);MatchCollectiontrmatches=Other.GetRegValues("<tr><td((?!</tr>).)+",form2);foreach(Matchmatchintrmatches){MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",match.Value);stringa=Other.GetRegValue(@".*",matches[1].Value.Replace("<tdalign='center'>&nbsp;",""));stringb=Other.GetRegValue(@".*",matches[2].Value.Replace("<tdalign='center'>&nbsp;",""));stringc=Other.GetRegValue(@".*",matches[3].Value.Replace("<tdalign='center'>&nbsp;",""));stringd=Other.GetRegValue(@".*",matches[4].Value.Replace("<td><spanclass='down'>",""));MessageBox.Show(a);//这里获取到了2次数据,循环。result=newGuoJiPrice(a,b,c,d,now);}}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}returnresult;}

返回写入数据库方法的时候,只获取到了一次数据,默认第一条数据publicstaticboolAddGuoJiPrice(intId,intClassId,GuoJiPriceguojiprices)//国际市场价格写入{try{MessageBox.Show(guojiprices.ZhongLiang);//这里只取到一次数据,也就是说跟没循环一样stringsql=string.Format("insertintoGuoJiJiaGe(ClassId,SmallId,Addtime,ZhongLiang,JiaGe,ChanDi,ZhangDie)values({1},{0},#{2}#)",ClassId,Id,guojiprices.AddTime);OleDbCommandcmd=newOleDbCommand(sql,con);returncmd.ExecuteNonQuery()>0;}catch(Exceptionex){Console.WriteLine(ex.Message);returnfalse;}}

解决方案十五:
你必须要判断每一个条件,每一种情况都要有返回值。

时间: 2024-08-31 13:33:11

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

在线等,并非所有的代码路径都返回值问题

问题描述 for(intm=0;m<alSchedule.Count;m++){if(schedule.Equals(alSchedule[m])){returntrue;}else{returnfalse;}} alSchedule是一个存放数组的List<int[]>List用来判断数组list是否存在一个数组schedule没发现什么错误啊.... 解决方案 解决方案二:for(intm=0;m<alSchedule.Count;m++){if(schedule.Equals

c# 方法 foreach-C#并非所有的代码路径都返回值

问题描述 C#并非所有的代码路径都返回值 public static string aa(string a) { string str; if (str == "") { MessageBox.Show("请输入..."); return null; } else { string[] split = Regex.Split(str, ";", RegexOptions.IgnoreCase); foreach (string s in spli

方法-C# 并非所有的代码路径都返回值

问题描述 C# 并非所有的代码路径都返回值 public static string aa(string a) { if (a == "") { MessageBox.Show("请输入..."); return null; } else { string[] split = Regex.Split(a, ";", RegexOptions.IgnoreCase); foreach (string s in split) { return s;

asp.net(C#)并非所有的代码路径都返回值

问题描述 asp.net(C#)并非所有的代码路径都返回值 public bool IsExist(string artNo1) { string artNo; List<string> list_artNo = new List<string>(); foreach (GridViewRow rows in GridView1.Rows) { artNo = rows.Cells[0].Text; list_artNo.Add(artNo); } foreach (string

错误 1 “admin_TeacherInfo.GetKmName(int)”: 并非所有的代码路径都返回值

问题描述 求教高手这个是什么原因,要怎么样改....错误代码如下:publicstringGetKmName(intnum){SqlConnectionconn=BaseClass.DBCon();conn.Open();SqlCommandcmd=newSqlCommand("selectLessonNamefromtb_LessonwhereID="+num,conn);try{stringkname=cmd.ExecuteScalar().ToString();returnkna

Win7安装错误提示与解决办法

很多不明真相的朋友安装windows7之后,会出现错误提示,而且大部分人都不知道该怎么去解决.通常windows7的错误提示会采取序号加数字的方式,依据惯例,这些需要都有他特殊的含义,各个版本的都是一样的.Win7安装错误提示和解决方法如下: 序号:1 原文:Pleasewait- 翻译:请稍候- 解决办法:这是安装开始时安装程序加载时的提示语.如果卡在这个地方,请检查硬件是否正常,如果正常请检查MD5是否一致.如果配置低,可能要多等一下,请耐心等待.在PE2.0以下在这一步会提示无法安装,原因

jsp页面调用oracle存储过程为什么总是报ORA-08103: object no longer exists 错误提示呢?

问题描述 jsp页面调用oracle存储过程为什么总是报ORA-08103:objectnolongerexists错误提示呢?调用代码:{callINDI_QUERY_AAA(?)}我在别的贴上看到这样答案:就是如果oracle存储过程中有insert,update,delete这些语句并且有返回cursor这样的类型,需要显式设置conn.setAutoCommit(false),在调用过程后设置为conn.setAutoCommit(true);就可以了.我的存储过程中也有insert,u

sql2000 1053错误-sql2000打补丁没成功,结果sql也进不去了,提示1053

问题描述 sql2000打补丁没成功,结果sql也进不去了,提示1053 服务没有及时响应启动或控制请求,此时正在mssqlserver服务上执行该服务操作. 怎么解决啊?? 解决方案 sql server 2000不兼容windows 7或者之后的版本.对于windows xp/2003,需要sp4.如果是前述的版本,还是换更高的sql server.对于后者,检查你的系统是不是山寨盗版.山寨盗版精简了文件,有各种问题. 建议你安装原版系统. 解决方案二: http://www.jb51.ne

使用u盘时弹出错误提示0x80070570解决方法

  使用u盘时弹出错误提示0x80070570解决方法来啦~相信很多朋友在使用U盘都遇到了错误提示0x80070570,遇到这情况该怎么解决呢?下文小编就为大家带来使用u盘时弹出错误提示0x80070570的解决方法,一起去了解下吧. 使用u盘时弹出错误提示0x80070570解决方法: 1.先检查u盘是否能够正常被电脑识别到,如果可以识别到那么再确认u盘中的文件数据是否重要,如果不重要,那么重新格式化u盘即可解决问题; 2.如果u盘中有重要文件时那么再尝试使用下面的操作解决问题: ①.将鼠标移