|
楼主 |
发表于 2019-9-29 20:08
|
显示全部楼层
见笑了。下面的代码是提取乡、村两级的。
Sub test()
Dim arr(), brr()
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.navigate "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/13/06/130606.html"
Do Until .readyState = 4
DoEvents
Loop
n = 0
For Each tr In .Document.body.all.tags("tr")
If (InStr(1, tr.className, "towntr") > 0 And tr.all.tags("a").Length > 0) Then
n = n + 1
ReDim Preserve arr(1 To 2, 1 To n)
arr(1, n) = tr.all.tags("td")(1).innerText
arr(2, n) = tr.all.tags("a")(0).href
End If
Next
q = 0
For i = 1 To n
.navigate arr(2, i)
Do Until .readyState = 4
DoEvents
Loop
For Each tr In .Document.body.all.tags("tr")
If (InStr(1, tr.className, "villagetr") > 0) Then
q = q + 1
ReDim Preserve brr(1 To 2, 1 To q)
brr(1, q) = arr(1, i)
brr(2, q) = tr.all.tags("td")(2).innerText
End If
Next
Next
End With
Cells(1, 1).Resize(UBound(brr, 2), UBound(brr)) = Application.Transpose(brr)
Columns("A:B").EntireColumn.AutoFit
End Sub |
|