|
技痒,重编了一下
- Sub GetUrlData()
- Dim sURL As String, sCode As String, oRegExp As Object, nPage As Long, nPages As Long, nRecords As Long, sResponseText As String
- Dim vData As Variant, nI As Long, nJ As Long, oReg As Object, vFill As Variant, nRow As Double, nCol As Long
-
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Cells.Clear
- sCode = InputBox("请输入代码", "提示", 180031)
- Set oRegExp = CreateObject("VBSCRIPT.REGEXP")
- oRegExp.Global = True
-
- nPage = 1
- sURL = "http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=" & sCode & "&page=[Page]&per=2000"
- With CreateObject("msXML2.ServerXMLHttp")
- Do While nPage = 1 Or nPage <= nPages
- .Open "GET", Replace(sURL, "[Page]", nPage), True
- .Send
- While .ReadyState <> 4
- DoEvents
- Wend
-
- sResponseText = .ResponseText
- With oRegExp
- If nPages = 0 Then
- .Pattern = "records[^\d]+(\d+)[^\d]+pages[^\d]+(\d+)"
- Set oReg = .Execute(sResponseText)
- If oReg.Count > 0 Then
- nRecords = Val(oReg(0).SubMatches(0))
- nPages = Val(oReg(0).SubMatches(1))
- End If
- .Pattern = "<th([^>]+)?>([^<]+)<"
- Set oReg = .Execute(sResponseText)
- If oReg.Count > 0 Then
- ReDim vFill(1 To nRecords + 1, 1 To oReg.Count + 1)
- vFill(1, 1) = sCode
- For nCol = 1 To oReg.Count
- vFill(1, nCol + 1) = oReg(nCol - 1).SubMatches(1)
- Next
- End If
- nCol = 8
- nRow = 1
- .Pattern = "<td([^>]+)?>([^<]+)?<"
- End If
- Set oReg = .Execute(sResponseText)
- If oReg.Count > 0 Then
- nI = 0
- Do While nI + 1 < oReg.Count
- sCode = oReg(nI).SubMatches(1)
- If sCode Like "*-*-*" Then
- nCol = 2
- nRow = nRow + 1
- vFill(nRow, 1) = nRow - 1
- Else
- nCol = nCol + 1
- End If
- vFill(nRow, nCol) = oReg(nI).SubMatches(1)
- nI = nI + 1
- Loop
- End If
- End With
- nPage = nPage + 1
- Loop
- End With
-
- With [A1].Resize(UBound(vFill), UBound(vFill, 2))
- .Offset(, 1).Resize(, 1).NumberFormatLocal = "yyyy-m-d"
- .Offset(, 2).Resize(, 2).NumberFormatLocal = "_ * #,##0.0000_ ;_ * -#,##0.0000_ ;_ * ""-""????_ ;_ @_ "
- .Offset(, 4).Resize(, 1).NumberFormatLocal = "0.00%"
- .Formula = vFill
- .EntireColumn.AutoFit
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlBottom
- End With
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- MsgBox "ok"
- End Sub
复制代码 |
评分
-
4
查看全部评分
-
|