以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  获取网页数据  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=74415)

--  作者:kunmails
--  发布时间:2015/9/10 13:05:00
--  获取网页数据
有没有办法获取国家统计局网站上的行政区域划分数据,在网上找了实在找不到最新的数据!

http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/index.html
--  作者:大红袍
--  发布时间:2015/9/10 15:00:00
--  

用下面代码慢慢获取。一次性获取会很慢,红色代码慢慢注释掉,理解代码。

 

mark 获取网页数据

 

Dim url As String = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/"
For i As Integer = 11 To 65
    Dim web As new System.Windows.Forms.WebBrowser
    web.Navigate(url & i & ".html")
    Do While web.ReadyState <> 4
        Application.DoEvents
    Loop
    Dim trs = web.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
    For j As Integer = 1 To trs.count - 1
        Dim tr = trs(j)
        Dim tds = tr.GetElementsByTagName("td")
        output.show(tds(0).innerText)
        output.show(tds(1).innerText)
        output.show(tds(1).GetElementsByTagName("a")(0).GetAttribute("href"))
        Dim web1 As new System.Windows.Forms.WebBrowser
        web1.Navigate(tds(1).GetElementsByTagName("a")(0).GetAttribute("href"))
        Do While web1.ReadyState <> 4
            Application.DoEvents
        Loop
        Dim trs1 = web1.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
        For j1 As Integer = 1 To trs1.count - 1
            Dim tr1 = trs1(j1)
            Dim tds1 = tr1.GetElementsByTagName("td")
            output.show(tds1(0).innerText)
            output.show(tds1(1).GetElementsByTagName("a")(0).GetAttribute("href"))
            Dim web2 As new System.Windows.Forms.WebBrowser
            web2.Navigate(tds1(1).GetElementsByTagName("a")(0).GetAttribute("href"))
            Do While web2.ReadyState <> 4
                Application.DoEvents
            Loop
            Dim trs2 = web2.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
            For j2 As Integer = 1 To trs2.count - 1
                Dim tr2 = trs2(j2)
                Dim tds2 = tr2.GetElementsByTagName("td")
                output.show(tds2(0).innerText)
                output.show(tds2(1).GetElementsByTagName("a")(0).GetAttribute("href"))
                Dim web3 As new System.Windows.Forms.WebBrowser
                web3.Navigate(tds2(1).GetElementsByTagName("a")(0).GetAttribute("href"))
                Do While web3.ReadyState <> 4
                    Application.DoEvents
                Loop
                Dim trs3 = web3.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
                For j3 As Integer = 1 To trs3.count - 1
                    Dim tr3 = trs3(j3)
                    Dim tds3 = tr3.GetElementsByTagName("td")
                    output.show(tds3(0).innerText)
                    output.show(tds3(1).innerText)
                    output.show(tds3(2).innerText)
                Next
                \'web3 = Nothing
                web3.Dispose

return ""
            Next
            \'web2 = Nothing
            web2.Dispose

return ""
        Next
        \'web1 = Nothing
        web1.Dispose

return ""
    Next
    web.Dispose

return ""
Next

 


--  作者:花开的声音
--  发布时间:2015/9/11 9:03:00
--  
回复收藏
--  作者:逛逛
--  发布时间:2015/9/11 9:42:00
--  
画个圈圈,备用。
--  作者:lsy
--  发布时间:2015/12/29 16:55:00
--  
能用得着,好。
--  作者:time
--  发布时间:2016/1/1 9:58:00
--  
请教楼主:获取的数据直接导入到表吗?内部表分别有哪些列表名是什么?上列代码是属于单击按钮事件吗?
--  作者:大红袍
--  发布时间:2016/1/3 23:12:00
--  
以下是引用time在2016/1/1 9:58:00的发言:
请教楼主:获取的数据直接导入到表吗?内部表分别有哪些列表名是什么?上列代码是属于单击按钮事件吗?

 

不是,获取到的数据,只显示在命令窗口里。

 

你要看懂代码,然后把output.show改成增加行和赋值的代码即可。


--  作者:whyxd
--  发布时间:2017/5/15 10:02:00
--  [求助]回复:(大红袍)用下面代码慢慢获取。一次性获取会很...

这个代码我测试了,很好用,只是有一点我不会解决,就是当市辖区没有下级连接的时候,会报错,比如山东省--济南市---市辖区,市辖区是最后一级,就会报错。还望指教!
图片点击可在新窗口打开查看此主题相关图片如下:bcjt111.png
图片点击可在新窗口打开查看


--  作者:有点色
--  发布时间:2017/5/15 10:57:00
--  

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=87832&skin=0

 


--  作者:zoudezhao
--  发布时间:2018/1/15 18:20:00
--  

Dim url As String = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/"
For i As Integer = 13 To 14
    Dim web As new System.Windows.Forms.WebBrowser
    web.Navigate(url & i & ".html")
    Do While web.ReadyState <> 4
        Application.DoEvents
    Loop
    Dim trs = web.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
    For j As Integer = 1 To trs.count - 1
        Dim tr = trs(j)
        Dim tds = tr.GetElementsByTagName("td")
        output.show(tds(0).innerText) \'二级ID
        output.show(tds(1).innerText) \'二级
        \' output.show(tds(1).GetElementsByTagName("a")(0).GetAttribute("href"))
        Dim web1 As new System.Windows.Forms.WebBrowser
        web1.Navigate(tds(1).GetElementsByTagName("a")(0).GetAttribute("href"))
        Do While web1.ReadyState <> 4
            Application.DoEvents
        Loop
        Dim trs1 = web1.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
        For j1 As Integer = 1 To trs1.count - 1
            Dim tr1 = trs1(j1)
            Dim tds1 = tr1.GetElementsByTagName("td")
            output.show(tds1(0).innerText) \'三级ID
            output.show(tds1(1).innerText) \'三级
            \' output.show(tds1(1).GetElementsByTagName("a")(0).GetAttribute("href"))
            If tds1(1).GetElementsByTagName("a").Count > 0 Then
                Dim web2 As new System.Windows.Forms.WebBrowser
                web2.Navigate(tds1(1).GetElementsByTagName("a")(0).GetAttribute("href"))
                Do While web2.ReadyState <> 4
                    Application.DoEvents
                Loop
                Dim trs2 = web2.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
                For j2 As Integer = 1 To trs2.count - 1
                    Dim tr2 = trs2(j2)
                    Dim tds2 = tr2.GetElementsByTagName("td")
                    output.show(tds2(0).innerText) \'四级ID
                    output.show(tds2(1).innerText) \'四级
                    \'  output.show(tds2(1).GetElementsByTagName("a")(0).GetAttribute("href"))
                    If tds2(1).GetElementsByTagName("a").Count > 0 Then
                        Dim web3 As new System.Windows.Forms.WebBrowser
                        web3.Navigate(tds2(1).GetElementsByTagName("a")(0).GetAttribute("href"))
                        Do While web3.ReadyState <> 4
                            Application.DoEvents
                        Loop
                        Dim trs3 = web3.Document.GetElementsByTagName("table")(4).GetElementsByTagName("tr")
                        For j3 As Integer = 1 To trs3.count - 1
                            Dim tr3 = trs3(j3)
                            Dim tds3 = tr3.GetElementsByTagName("td")
                            Dim r As Row = Tables("全国行政区").AddNew
                            r("省级ID") = i
                            r("二级ID") = tds(0).innerText
                            r("三级ID") = tds1(0).innerText
                            r("四级ID") = tds2(0).innerText
                            r("五级ID") = tds3(0).innerText
                            r("二级") = tds(1).innerText
                            r("三级") = tds1(1).innerText
                            r("四级") = tds2(1).innerText
                            r("五级") = tds3(2).innerText
                            output.show(tds3(0).innerText) \'五级ID
                            \'   output.show(tds3(1).innerText) \'五级
                            output.show(tds3(2).innerText)
                        Next
                        \'web3 = Nothing
                        web3.Dispose
                       
             \'           Return ""
                    Else
                        Dim r As Row = Tables("全国行政区").AddNew
                        r("省级ID") = i
                        r("二级ID") = tds(0).innerText
                        r("三级ID") = tds1(0).innerText
                        r("四级ID") = tds2(0).innerText
                        r("二级") = tds(1).innerText
                        r("三级") = tds1(1).innerText
                        r("四级") = tds2(1).innerText
                    End If
                Next
                \'web2 = Nothing
                web2.Dispose
               
           \'     Return ""
            Else
                Dim r As Row = Tables("全国行政区").AddNew
                r("省级ID") = i
                r("二级ID") = tds(0).innerText
                r("三级ID") = tds1(0).innerText
                r("二级") = tds(1).innerText
                r("三级") = tds1(1).innerText
            End If
        Next
        \'web1 = Nothing
        web1.Dispose
       
    \'    Return ""
    Next
    web.Dispose
   
 \'   Return ""
Next

 

 

 

 


图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20180115181346.png
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:qq截图20180115173841.png
图片点击可在新窗口打开查看

 

正常的五级分类为什么会报错,哪位老师帮忙看下。。。图片点击可在新窗口打开查看