|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
n_max指定页数 不指定全部下载
- Sub tet()
- Dim strText As String, t, i&, j&, TR, TD, n&
- Dim reg, n_max&, viewstate$
- n_max = 10
- Set reg = CreateObject("vbscript.regexp")
- reg.Global = True
- reg.Pattern = "共(\d+)页"
- Application.ScreenUpdating = False
- Sheet1.Cells.ClearContents
- Sheet1.Range("A:A").NumberFormatLocal = "@"
- With CreateObject("WinHttp.WinHttpRequest.5.1")
- Do
- n = n + 1
- .Open "POST", "http://59.49.34.87:2016/Application/gongs.aspx", False
- .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
- If n = 1 Then
- .Send "GridView1%24ctl53%24txtGoPage=1"
- Else
- .Send "__VIEWSTATE=" & viewstate & "&GridView1%24ctl53%24txtGoPage=" & n
- End If
- strText = .responsetext
- With CreateObject("htmlfile")
- .write strText
- viewstate = encodeURI(.getElementById("__VIEWSTATE").Value)
- t = 0
- For Each TR In .all.tags("table")("GridView1").Rows
- t = t + 1
- If (t = 1 And n = 1) Or t > 1 Then
- i = i + 1: j = 0
- For Each TD In TR.Cells
- If n = 1 Then
- If reg.test(TD.innertext) Then
- n_max = IIf(Val(reg.Execute(TD.innertext)(0).submatches(0)) > n_max, n_max, Val(reg.Execute(TD.innertext)(0).submatches(0)))
- End If
- End If
- If reg.test(TD.innertext) Then i = i - 1: Exit For
- j = j + 1
- Sheet1.Cells(i, j) = TD.innertext
- Next
- End If
- Next
- End With
- If n = n_max Then Exit Do
- Loop
- End With
- Application.ScreenUpdating = True
- End Sub
- Function encodeURI(strTobecoded As String) As String
- With CreateObject("msscriptcontrol.scriptcontrol")
- .Language = "JavaScript"
- encodeURI = .Eval("encodeURIComponent('" & strTobecoded & "');")
- 'encodeURIComponent无法转换括号,所以再替换下括号
- encodeURI = Replace(Replace(encodeURI, "(", "%28"), ")", "%29")
- End With
- End Function
复制代码 |
|