|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
做好了一键提取所有省份的数据,请测试代码:
- Public Sub test()
- Dim Str$, url$, i%, n%, j%, db, tr, td, Str2$, db2, tr2, td2
- Dim html As Object, arr, brr, Reg, mh, k, r%
- Set Reg = CreateObject("vbscript.regexp")
- Set html = CreateObject("htmlfile")
- Application.ScreenUpdating = False
- url = "http://www.eol.cn/html/gkcx/jh2015/31/125.htm"
- With CreateObject("msxml2.xmlhttp")
- .Open "GET", url, False
- .send
- Str = .responsetext
- '-------------------------------------------------------
- Str2 = Split(Split(Str, "<table width=""93%"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">")(1), "</table>")(0)
- Str2 = "<table width=""93%"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">" & Str2
- With Reg
- .Pattern = "<a.*/([0-9]{2})/[^一-龥]+([一-龥]{2,5}){1}</a>" '
- ''<a href="../34/125.htm">安徽</a></
- .Global = True
- Set mh = Reg.Execute(Str2)
- ReDim brr(1 To mh.Count, 1 To 2)
- For Each k In mh
- n = n + 1
- brr(n, 2) = k.submatches(0)
- brr(n, 1) = k.submatches(1)
- Next
- End With
- '---------------------------------------------------------
- Cells.Clear
- For r = 1 To n
- url = "http://www.eol.cn/html/gkcx/jh2015/" & brr(r, 2) & "/125.htm"
- .Open "GET", url, False
- .send
- Str = .responsetext
- html.body.innerhtml = Str
- Set db = html.all.tags("table")("demotable1") '<table id="demotable1">
- i = 0: n = 0: n = db.Rows.Length
- ReDim arr(1 To n, 1 To 8)
- For Each tr In db.Rows
- i = i + 1: j = 0
- For Each td In tr.Cells
- j = j + 1
- arr(i, j) = td.innertext
- Next
- Next
- If r = 1 Then
- Range("A65536").End(3) = brr(r, 1)
- Else
- Range("A65536").End(3).Offset(1) = brr(r, 1)
- End If
- Range("A65536").End(3).Offset(1).Resize(UBound(arr), UBound(arr, 2)) = arr
- Set db = Nothing
- Next
- End With
- Application.ScreenUpdating = True
- MsgBox "完成!"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|