|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub downloadSohuData()
Dim strQuery As String
Dim txtContent As String
Dim httpreq As XMLHTTP30
Dim arrTemp() As Variant
Dim arrT As Variant
Dim arrT1 As Variant
Dim strCode As String
Dim i As Integer
Dim j As Integer
Dim dDate As Date
Dim iFound
Dim iRow As Integer
Dim strTemp As String
Application.ScreenUpdating = False
If Cells(2, 1) = "" Then Exit Sub
iRow = Range("A1").End(xlDown).Row
Set httpreq = New XMLHTTP30
For j = 2 To iRow
Application.StatusBar = Format((j - 1) / (iRow - 1) * 100, "0.00") & "% finished..."
strCode = Cells(j, 1)
If Len(strCode) = 4 Then
strQuery = "http://hqk.stock.sohu.com/hk/" & Right(strCode, 3) & "/hk_0" & strCode & "-1.html"
Else
strQuery = "http://hq.stock.sohu.com/cn/" & Right(strCode, 3) & "/cn_" & strCode & "-1.html"
End If
httpreq.Open "GET", strQuery, False
httpreq.setRequestHeader "Content-Type", "text/html"
httpreq.send
If httpreq.Status = 200 Then
txtContent = httpreq.responseText
txtContent = Mid(txtContent, InStr(1, txtContent, "</script>") + 9)
txtContent = Left(txtContent, InStr(1, txtContent, "</script>"))
arrT = Split(txtContent, """,""")
If UBound(arrT) = 0 Then
arrT = Split(txtContent, "','")
End If
Cells(j, 2) = arrT(1)
Cells(j, 3) = arrT(2)
strTemp = arrT(UBound(arrT))
If Left(strTemp, 1) <> """" Or Left(strTemp, 1) <> "'" Then
If InStr(1, strTemp, """") = 0 Then
Cells(j, 4) = Left(strTemp, InStr(1, strTemp, "'") - 1)
Else
Cells(j, 4) = Left(strTemp, InStr(1, strTemp, """") - 1)
End If
End If
Else
reportErr httpreq.Status
End If
Next j
httpreq.abort
Application.StatusBar = False
Application.ScreenUpdating = True
Set httpreq = Nothing
End Sub
Sub reportErr(lStatus As Integer)
Select Case lStatus
Case 400
MsgBox "Bad Request", vbCritical, "连接错误"
Case 401
MsgBox "Unauthorized", vbCritical, "连接错误"
Case 402
MsgBox "Payment Required", vbCritical, "连接错误"
Case 403
MsgBox "Forbidden", vbCritical, "连接错误"
Case 404
MsgBox "Not Found", vbCritical, "连接错误"
Case 407
MsgBox "Proxy Authentication Required", vbCritical, "连接错误"
Case 408
MsgBox "Request Timeout", vbCritical, "连接错误"
Case 503
MsgBox "Service Unavailable", vbCritical, "连接错误"
Case Else
MsgBox "Can not reach by other reason", vbCritical, "连接错误"
End Select
End Sub
请老师帮忙看一下,谢谢。
|
|