以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 求助:多页网页代码获取时,提示错误! (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=92376) |
-- 作者:李孝春 -- 发布时间:2016/11/2 10:47:00 -- 求助:多页网页代码获取时,提示错误! 代码如下: Dim web As new System.Windows.Forms.WebBrowser web.ScriptErrorsSuppressed = True web.Navigate("http://www.gzwengan.jcy.gov.cn/gzdt/") Do Until web.ReadyState = 4 Application.DoEvents Loop Dim obj = web.document.GetElementsByTagName("table")(37) \'页面代码TABLE的一共有82个 含<table></table> Dim count = obj.InnerText.Substring(1,1) output.Show(count) Dim str As String = "" For i As Integer = 1 To count Dim pg = web.Document.GetElementById("num") pg.SetAttribute("Value", i) Dim inputs = web.Document.GetElementsByTagName("input") For Each input As object In inputs If input.GetAttribute("type") = "submit" Then input.InvokeMember("click") Do Until web.document.GetElementsByTagName("table").count > 36 Application.DoEvents Loop Dim tb1s = web.document.GetElementsByTagName("table")(36) \'页面代码TABLE的一共有82个 含<table></table>即从第36个<Table>开始查找 For Each tb1 As object In tb1s.GetElementsByTagName("tr")\'遍历TR\' Dim sa = tb1.GetElementsByTagName("a")\'获取A标签元素\' If sa.count > 0 Then Dim a = sa(0) str &= a.InnerHtml str &= a.GetAttribute("href") Dim shijian As String= (FileSys.GetName(a.GetAttribute("href")).Insert(5,"-")).Insert(8,"-") Dim Parts() As String = shijian.Split("_") str & =Parts(0).Remove(0,1) End If Next End If Next Next output.Show(str) 错误如下: 如果将代码去掉“下一页”所有操作代码如下: Dim web As new System.Windows.Forms.WebBrowser web.ScriptErrorsSuppressed = True web.Navigate("http://www.gzwengan.jcy.gov.cn/gzdt/") Do Until web.ReadyState = 4 Application.DoEvents Loop Dim obj = web.document.GetElementsByTagName("table")(37) \'页面代码TABLE的一共有82个 含<table></table> Dim count = obj.InnerText.Substring(1,1) output.Show(count) Dim str As String = "" Dim tb1s = web.document.GetElementsByTagName("table")(36) \'页面代码TABLE的一共有82个 含<table></table>即从第36个<Table>开始查找 For Each tb1 As object In tb1s.GetElementsByTagName("tr")\'遍历TR\' Dim sa = tb1.GetElementsByTagName("a")\'获取A标签元素\' If sa.count > 0 Then Dim a = sa(0) str &= a.InnerHtml str &= a.GetAttribute("href") Dim shijian As String= (FileSys.GetName(a.GetAttribute("href")).Insert(5,"-")).Insert(8,"-") Dim Parts() As String = shijian.Split("_") str & =Parts(0).Remove(0,1) End If Next output.Show(str) 运行结果如下: |
-- 作者:有点蓝 -- 发布时间:2016/11/2 11:35:00 -- 这边测试么有问题 |
-- 作者:有点蓝 -- 发布时间:2016/11/2 11:36:00 -- Dim count as integer = val(obj.InnerText.Substring(1,1)) |
-- 作者:有点色 -- 发布时间:2016/11/2 11:39:00 -- Dim web As new System.Windows.Forms.WebBrowser web.ScriptErrorsSuppressed = True web.Navigate("http://www.gzwengan.jcy.gov.cn/gzdt/") Do Until web.ReadyState = 4 AndAlso web.document.GetElementsByTagName("table").count > 37 Application.DoEvents Loop Dim tbs As object = web.document.GetElementsByTagName("table") \'For i As Integer = 0 To tbs.count-1 \'output.show(i & " " & tbs(i).InnerText) \'Next Dim obj = tbs(37) \'页面代码TABLE的一共有82个 含<table></table> \'output.show(obj.InnerHtml) Dim strKey As String = "</script>" Dim idx As Integer = obj.InnerHtml.IndexOf(strKey) Dim count = obj.Innerhtml.Substring(idx+strKey.Length+1,2).replace("页", "") msgbox(count) Dim str As String = "" For i As Integer = 1 To count Dim pg = web.Document.GetElementById("num") pg.SetAttribute("Value", i) Dim inputs = web.Document.GetElementsByTagName("input") For Each input As object In inputs If input.GetAttribute("type") = "submit" Then input.InvokeMember("click") Do Until web.document.GetElementsByTagName("table").count > 36 Application.DoEvents Loop Dim tb1s = web.document.GetElementsByTagName("table")(36) \'页面代码TABLE的一共有82个 含<table></table>即从第36个<Table>开始查找 For Each tb1 As object In tb1s.GetElementsByTagName("tr")\'遍历TR\' Dim sa = tb1.GetElementsByTagName("a")\'获取A标签元素\' If sa.count > 0 Then Dim a = sa(0) str &= a.InnerHtml str &= a.GetAttribute("href") Dim shijian As String= (FileSys.GetName(a.GetAttribute("href")).Insert(5,"-")).Insert(8,"-") Dim Parts() As String = shijian.Split("_") str & =Parts(0).Remove(0,1) End If Next End If Next Next output.Show(str) |