以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助] 网页值没有时怎么设置返回值 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=124139) |
-- 作者:hrlong -- 发布时间:2018/8/31 10:10:00 -- [求助] 网页值没有时怎么设置返回值 老师好,我采用以下代码,采集股票数据;但是有些股票因为停牌,退市;会查找不到数据,这样的话 用这个代码采集数据会出现“索引超出了数组界限。”这样的错误; 请问怎么修改代码,让采集的数据为空时,自动返回0; ========================================================== Dim obj_down As New System.Net.WebClient() Dim drs As List(Of DataRow) = DataTables("表A").Select("股票代码 is not null") For Each dr As DataRow In drs Dim data As String = obj_down.DownloadString("http://hq.sinajs.cn/list=" & dr("股票代码")) Dim start As Integer = data.IndexOf("""") + 1 Dim en As Integer = data.IndexOf(""";") Dim datas() As String = data.SubString(start, en - start).Split(",") dr("股票名字") = datas(0) dr("今日开盘") = datas(1) dr("昨日收盘") = datas(2) dr("当前价格") = datas(3) Next ============================================================= 正常时: var hq_str_sh603916="苏博特,14.900,14.900,14.910,15.050,14.730,14.900,14.910,1994900,29789704.000,13600,14.900,39800,14.890,13700,14.880,1400,14.870,2700,14.860,4100,14.910,2300,14.920,7100,14.930,2000,14.940,4400,14.950,2018-08-31,10:08:49,00"; 异常时: var hq_str_sh603297=""; |
-- 作者:有点甜 -- 发布时间:2018/8/31 10:20:00 -- 能否执行到下面代码?
Dim datas() As String = data.SubString(start, en - start).Split(",") msgbox(datas.length) |
-- 作者:hrlong -- 发布时间:2018/8/31 11:39:00 -- msgbox(datas.length) ============================== 正常的时候显示的是33;如果不正常的时候显示1,显示1以后,马上就会出现“索引超出了数组界限。”这样的错误报警
|
-- 作者:有点甜 -- 发布时间:2018/8/31 11:47:00 -- Dim obj_down As New System.Net.WebClient()
Dim drs As List(Of DataRow) = DataTables("表A").Select("股票代码 is not null")
For Each dr As DataRow In drs
Dim data As String = obj_down.DownloadString("http://hq.sinajs.cn/list=" & dr("股票代码"))
Dim start As Integer = data.IndexOf("""") + 1
Dim en As Integer = data.IndexOf(""";")
Dim datas() As String = data.SubString(start, en - start).Split(",")
If datas.length > 10 then
dr("股票名字") = datas(0)
dr("今日开盘") = datas(1)
dr("昨日收盘") = datas(2)
dr("当前价格") = datas(3)
Else
dr("股票名字") = "没找到" End If
Next
|