hỏi cách lấy dữ liệu từ bảng giá chứng khoán trực tuyến trên web vào file excel (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

hanphong_2008

Thành viên mới
Tham gia
8/2/09
Bài viết
28
Được thích
1
hỏi cách lấy dữ liệu từ bảng giá chứng khoán trực tuyến trên web vào file excel. Trước đây mình có lấy được mà giờ ko lấy được từ web bảng giá chứng khoán của công ty nào về file excel nữa, Có bạn nào làm được ko hướng dẫn mình với
 
@hanphong_2008,

Địa chỉ website đó là gì vậy?
 
Trước đây file excel của mình import dữ liệu từ bảng giá cũ này: http://liveprice.fpts.com.vn/user/stockE/HCM/?s=50&language
sau link này cty đã đổi ko dùng nữa lên ko lấy được dữ liệu. Giờ mình thử lấy lấy dữ liệu từ bảng giá của nhiều cty chứng khoán khác mà đều không được? Các bảng giá như https://www.hsx.vn/Modules/Rsde/RealtimeTable/LiveSecurity

http://priceboard.fpts.com.vn/?s=34&t=aAll

http://priceboard.acbs.com.vn/

http://banggia.chungkhoanphuongnam.com.vn/home/index?id=hsx

http://stockboard.sbsc.com.vn/apps/StockBoard/SBSC/HASE.html

http://banggia2.ssi.com.vn/Hnx.aspx

Tất cả đều không được ?
 
loi.jpg
có bạn nào giúp được mình ko nhỉ nó hiện lên lỗi thế này
 
MÌnh muốn lấy từ trang : http://priceboard.fpts.com.vn/?s=34&t=aAll
Lấy hết các cột mà nó hiện ra đó. Đó là trang mình thấy vừa ý nhất để dùng cho việc xử lí dữ liệu của mình

Hoặc lấy từ trang: https://www.hsx.vn/Modules/Rsde/RealtimeTable/LiveSecurity

trong file có cái nút bấm , tùy chọn sàn mà lấy nhé

Mã:
Public Sub hello(ByVal region As String, ByVal targetTB As String)
Dim arr(1 To 2000, 1 To 25), r As Long, str As String
Dim mats, mapID, mat, dArr, ub As Long, col As Long
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://priceboard.fpts.com.vn/" & region & "/data.ashx?s=quote&l=" & targetTB, False
    .send
    str = .ResponseText
End With
mapID = Array(1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
        16, 17, 18, 19, -1, 20, 21, 22, 23, -1, 24, 25)
ub = UBound(mapID) + 1
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\[""?(\d{1,2})""?,""?([^\]""]+)""?"
    dArr = Split(str, "}")
    For r = 0 To UBound(dArr)
        Set mats = .Execute(dArr(r))
        For Each mat In mats
            col = mat.submatches(0)
            If col < ub Then
                If mapID(col) > 0 Then
                    arr(r + 1, mapID(col)) = mat.submatches(1)
                End If
            End If
        Next
    Next
End With
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
 

File đính kèm

Thanks bạn @AutoReply rất nhiều nhé. Mình mượn tạm file bạn để dùng xử lí dữ liêu của mình nhé, mình chưa học về vba nên ko hiểu gì để tùy biến nó trên file của mình cả. Mình sẽ học thêm dần vậy. Cám ơn sự giúp đỡ nhé hi /-*+/
 
Công ty chứng khoán của FPT lại ko có lịch sử bảng giá chứng khoán của các ngày trước đó thì phải ? Chỉ có bảng giá của ngày hiện tại. Mình muốn lấy bảng giá của các ngày trước đó thì phải làm thế nào.

Hiện mình biết chỉ có công ty chứng khoán bản việt bảng giá có chức năng này http://priceboard1.vcsc.com.vn/vcsc/history. Nhưng dữ liệu các cột lại sắp xếp khác với bảng giá của công ty chứng khoán FPT.

Nếu mình muốn lấy được dữ liệu từ nó nhưng các cột được sắp xếp lại như bảng giá của FPT http://priceboard.fpts.com.vn/?s=34&t=aAll thì phải làm như thế nào. Giúp mình với
 
Công ty chứng khoán của FPT lại ko có lịch sử bảng giá chứng khoán của các ngày trước đó thì phải ? Chỉ có bảng giá của ngày hiện tại. Mình muốn lấy bảng giá của các ngày trước đó thì phải làm thế nào.

Hiện mình biết chỉ có công ty chứng khoán bản việt bảng giá có chức năng này http://priceboard1.vcsc.com.vn/vcsc/history. Nhưng dữ liệu các cột lại sắp xếp khác với bảng giá của công ty chứng khoán FPT.

Nếu mình muốn lấy được dữ liệu từ nó nhưng các cột được sắp xếp lại như bảng giá của FPT http://priceboard.fpts.com.vn/?s=34&t=aAll thì phải làm như thế nào. Giúp mình với

Tuần này tôi khá bận , đành nhờ các bạn khác diễn đàn giúp vậy .
 
Web fpts đó cập nhật chậm và thường ko đầy đủ. Em muốn nhờ các anh chị giúp đỡ lấy dữ liệu từ 3 tab HOSE, HNX, UPCOM trên website
http://quotes.vcbs.com.vn/priceboard
về excel ạ.
Rất mong được giúp đỡ, em cảm ơn nhiều ạ!
 
ủa bạn vẫn đang chờ sao ? Tôi tưởng bỏ đi luôn rồi.
Theo như tôi quan sát thì trang vcbs có cách tổ chức website khá là cũ kĩ cồng kềnh, không giống như trang fpt ở trên, nếu thật sự cần thiết thì tôi vẫn làm được, bạn có thể chờ vài ba bữa nhé, vì tôi không có nhiều thời gian ban ngày, chỉ ráng tranh thủ vài buổi tối thôi à.
 
trong file có cái nút bấm , tùy chọn sàn mà lấy nhé

Mã:
Public Sub hello(ByVal region As String, ByVal targetTB As String)
Dim arr(1 To 2000, 1 To 25), r As Long, str As String
Dim mats, mapID, mat, dArr, ub As Long, col As Long
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://priceboard.fpts.com.vn/" & region & "/data.ashx?s=quote&l=" & targetTB, False
    .send
    str = .ResponseText
End With
mapID = Array(1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
        16, 17, 18, 19, -1, 20, 21, 22, 23, -1, 24, 25)
ub = UBound(mapID) + 1
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\[""?(\d{1,2})""?,""?([^\]""]+)""?"
    dArr = Split(str, "}")
    For r = 0 To UBound(dArr)
        Set mats = .Execute(dArr(r))
        For Each mat In mats
            col = mat.submatches(0)
            If col < ub Then
                If mapID(col) > 0 Then
                    arr(r + 1, mapID(col)) = mat.submatches(1)
                End If
            End If
        Next
    Next
End With
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
cái này bạn cho nó tự refresh khoảng 5 phút một lần, tức là mở file ra mình không phải bấm nó tự chạy luôn.
 
ủa bạn vẫn đang chờ sao ? Tôi tưởng bỏ đi luôn rồi.
Theo như tôi quan sát thì trang vcbs có cách tổ chức website khá là cũ kĩ cồng kềnh, không giống như trang fpt ở trên, nếu thật sự cần thiết thì tôi vẫn làm được, bạn có thể chờ vài ba bữa nhé, vì tôi không có nhiều thời gian ban ngày, chỉ ráng tranh thủ vài buổi tối thôi à.
Cảm ơn anh, em chờ được ạ. Em ko biết cấu trúc web như thế nào, nhưng thấy vcbs cập nhật số liệu kịp thời và đầy đủ, chính xác hơn ạ.
 
Cho mình hỏi thêm về bài toán này: Làm sao để lấy data của 3 sàn hose, hnx và upcom vào 3 sheet, và thêm 1 sheet để theo dõi các mã mình đầu tư!
 
Mong được các anh giành thời gian giúp đỡ :(
 
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub
 

File đính kèm

ồ thật vậy sao bạn ? Nhưng thôi mình không dám phản đối bạn đâu. Khi có quá ít thời gian thì đành phải công nhận như vậy, khỏe hơn. --=0--=0
í, hổng chịu đâu. :(:(
Bạn ở trên biểu là "nghĩ" như thế thôi.
Cô Rô bốt mà dỗi là bắt đền bạn ở trên. Hu hu.
 

File đính kèm

  • upload_2017-9-17_23-20-39.png
    upload_2017-9-17_23-20-39.png
    187.3 KB · Đọc: 248
  • upload_2017-9-17_23-21-35.png
    upload_2017-9-17_23-21-35.png
    132.8 KB · Đọc: 226
í, hổng chịu đâu. :(:(
Bạn ở trên biểu là "nghĩ" như thế thôi.
Cô Rô bốt mà dỗi là bắt đền bạn ở trên. Hu hu.

Tôi không biết gì hết, không hiểu gì hết. Giờ các bạn diễn đàn nói gì tôi cũng thấy đúng. Thế là nhẹ nhàng thanh thản. hi hi %#^#$%#^#$
 
Bạn @AutoReply mình hỏi xíu mình muốn lấy theo ngành với lại macro tự động cập nhật số mới của thị trường sau 3 phút hay 5 phút gì đó thì mình code mình nên viết ntn nhỉ? mình cũng không rành phần này lắm
Cảm ơn bạn nhiều
 
Em lần đầu tiên vào web e thấy bác @AutoReply siêu thật. E chưa bao giờ được học nhưng thấy các anh viết trên đây thật ngưỡng mộ các anh. Chúc các anh sức khoẻ và thành công.
 
cám ơn bác rất nhiều, em tìm cách link dữ liệu mãi mà không được.
 
bạn Autoreply ơi, chỉ giúp mình với. Mình ko thấy có nút bấm nào trên file cả, nút bấm đó ở đâu à bạn
 
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub


Chào bạn AutoReply!
Cảm ơn các code bạn viết. Mình sử dụng rất hay. Bạn có thể tách ra 3 sheets khác nhau HOSE, HNX, UPCOM trong file VCBS ko ah??? Vì mình thấy code của trang VCBS chạy nhanh hơn code FPTS( code FPTS trong giờ giao dịch khi chạy bị chậm rồi hiện lỗi "Timeout Error"
 
Lần chỉnh sửa cuối:
Sau khi tải về dùng được 1 thời gian thì xuất hiện lỗi như hình đính kèm, các bác cho em xin giải pháp với ạ!
Em dùng code bên file fpt.xlsb ạ!
 

File đính kèm

  • 2018-04-18_8-59-42.png
    2018-04-18_8-59-42.png
    51.3 KB · Đọc: 163
  • 2018-04-18_8-59-31.png
    2018-04-18_8-59-31.png
    6.1 KB · Đọc: 169
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub
Mình muốn thêm cái auto refresh thì làm thế nào nhỉ bạn :D
 
ồ thật vậy sao bạn ? Nhưng thôi mình không dám phản đối bạn đâu. Khi có quá ít thời gian thì đành phải công nhận như vậy, khỏe hơn. --=0--=0
Chào bạn AutoReply, bạn giúp mình đoạn code (chèn vào file của bạn) mà lấy được dữ liệu của chỉ số nữa không ạ? Chỉ số Vnindex, VN30,... Cám ơn bạn nhiều!!!
 


Dùng bảng của bác nó bị lỗi này: The operation timed out, bác có cách nào xử lý giúp e ko?

Trong giờ giao dịch Real Time nhẩy giá liên tục là bị, hết giờ bảng giá đứng im thì nó lại chẳng bị lỗi này nữa.... khổ ghê
 

File đính kèm

  • Error dowload bang gia.jpg
    Error dowload bang gia.jpg
    808 KB · Đọc: 123
Lần chỉnh sửa cuối:
Chào mọi người, hôm qua sau phiên giao dịch mình down về thì còn xài được nhưng trong phiên giao dịch sáng nay thì bị lỗi debug như sau. có cách nào để thay đổi sang website khác như https://trade-hn.vndirect.com.vn/chung-khoan/hose được ko ạ
 

File đính kèm

  • Capture.JPG
    Capture.JPG
    114.5 KB · Đọc: 97
các sư phụ cho e hỏi có tài liệu, giáo trình nào hướng dẫn sử dụng chi tiết món "CreateObject("MSXML2.ServerXMLHTTP")" này không ạ. trước e có tìm trong diễn đàn mình bài viết có món Dictionary, nhưng món CreateObject("MSXML2.ServerXMLHTTP không có trong diễn đàn mình ạ.nên mạo muội nhờ các sư phụ chỉ lối ^^^^
e cảm ơn!
 
Lần chỉnh sửa cuối:
có sư phụ nào có tài liệu không ạ. cho e xin với ><<...,
 
chào mọi người.
mọi người có biết lấy bảng giá chứng khoán theo nghành trực tuyến không giúp minh với

xin cam on moi nguoi ạ
 
Có cách nào lấy giá chứng khoán vào File ACCESS không bạn ?
 
dowload về máy xong báo lỗi timeout ko dùng đc
mong giúp đỡ
 
bạn AUTOREPLY ơi có thể chọn theo ngày tháng khác nhau được không, mình xin cám ơn !
 
xin chào anh chị,
em đã đọc các bình luận và giúp đỡ của mn, nhưng e vẫn ko hiểu làm thế nào để tải dữ liệu trong bảng trên link này ạ https://finance.yahoo.com/quote/^GSPC/history?p=^GSPC
anh chị nào có code xin giúp em với ạ :))
Trên Excel đời mới gần đây (2013, 2016, 2019, 365...) Bạn chỉ cần vào Tab Data -> From Web xong nhập địa chỉ trên vào rồi xử lý vài bước đơn giản rồi nó ra cái bảng bạn cần nhé.
 
Hi anh Auto Refresh, file này của anh có thẻ thêm 1 sheet bảng giá phải sinh và 1 sheet các chỉ số VNindex, Vn30, HNXindex, HNX30 không ạ. Nếu được phiền anh sửa giúp em nhé.

Em hôm nay mới tìm được topic này, mặc dù đúng vào phiên cuối tuần nên chưa thử chạy real time được nhưng em thâý file này rất hữu ích ạ.

Tks anh và mọi người nhé!
Bài đã được tự động gộp:

Hi anh Auto Refresh, file này của anh có thẻ thêm 1 sheet bảng giá phải sinh và 1 sheet các chỉ số VNindex, Vn30, HNXindex, HNX30 không ạ. Nếu được phiền anh sửa giúp em nhé.

Em hôm nay mới tìm được topic này, mặc dù đúng vào phiên cuối tuần nên chưa thử chạy real time được nhưng em thâý file này rất hữu ích ạ.

Tks anh và mọi người nhé!
 

File đính kèm

Fpts chặn priceboard rồi, giờ phải có tk mới vào được
 
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub
Cái này bạn có thể cho mỗi bảng của ba sàn lúc lấy về cho vào 3 sheet khác nhau được k?
 
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub

Có thể lấy dữ liệu bảng giá phái sinh và chứng quyền không anh ơi
 
Mình không biết lấy bạn ơi. Bạn liên hệ bài #64 nhé.
Bác ơi, cháu tìm trên internet có mỗi file của bác là lấy được dữ liệu bảng giá, nhưng chỉ lấy được dữ liệu một sàn nhất định mỗi lần.
Hiện cháu mong muốn lấy bảng giá trực tuyến, toàn bộ giữ liệu của mỗi sàn (3 sàn - tất cả các mã) vào chung một sheet/hoặc mỗi sàn vào một sheet cũng được ạ. (Nếu có thể thêm được auto refresh theo thời gian đặt thì tốt cho cháu lắm ạ).
Nhờ Bác có thể chỉnh giúp cháu được không ạ.
Đây là file của bác đã làm trong chủ đề ạ.
Cháu cám ơn các bác nhiều ạ!
 
Trên vcbs bị chặn rồi. Xin bác @AutoReply giúp với. Lỗi chắc ở khúc này. Đường link ko vào được nữa.

With CreateObject("MSXML2.ServerXMLHTTP")
.Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
.setRequestHeader "Content-Type", "application/json"
.send (payload)
str = .ResponseText
End With

XIN LỖI LÀM PHIỀN BÁC. ĐÃ TRUY CẬP LẠI BÌNH THƯỜNG RỒI Ạ.
 
Lần chỉnh sửa cuối:
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub

Bác ơi, trang bảng giá của VCBS mới thay đổi hôm nay (31/12/2020), giờ file này không update được nữa. Bác xem chỉnh sửa file giúp theo bảng giá mới của VCBS được không ạ. Cám ơn bác nhiều!
 
trong file có cái nút bấm , tùy chọn sàn mà lấy nhé

Mã:
Public Sub hello(ByVal region As String, ByVal targetTB As String)
Dim arr(1 To 2000, 1 To 25), r As Long, str As String
Dim mats, mapID, mat, dArr, ub As Long, col As Long
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://priceboard.fpts.com.vn/" & region & "/data.ashx?s=quote&l=" & targetTB, False
    .send
    str = .ResponseText
End With
mapID = Array(1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
        16, 17, 18, 19, -1, 20, 21, 22, 23, -1, 24, 25)
ub = UBound(mapID) + 1
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\[""?(\d{1,2})""?,""?([^\]""]+)""?"
    dArr = Split(str, "}")
    For r = 0 To UBound(dArr)
        Set mats = .Execute(dArr(r))
        For Each mat In mats
            col = mat.submatches(0)
            If col < ub Then
                If mapID(col) > 0 Then
                    arr(r + 1, mapID(col)) = mat.submatches(1)
                End If
            End If
        Next
    Next
End With
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
làm ơn nói kỹ
tôi chưa va cái này bao giờ
Bài đã được tự động gộp:

có ai biết tải thì giúp với ạ
 

vithong

Bác có thể tham khảo thêm ứng dụng tại bài viết ở link bên dưới:
 
trong file có cái nút bấm , tùy chọn sàn mà lấy nhé

Mã:
Public Sub hello(ByVal region As String, ByVal targetTB As String)
Dim arr(1 To 2000, 1 To 25), r As Long, str As String
Dim mats, mapID, mat, dArr, ub As Long, col As Long
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://priceboard.fpts.com.vn/" & region & "/data.ashx?s=quote&l=" & targetTB, False
    .send
    str = .ResponseText
End With
mapID = Array(1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
        16, 17, 18, 19, -1, 20, 21, 22, 23, -1, 24, 25)
ub = UBound(mapID) + 1
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\[""?(\d{1,2})""?,""?([^\]""]+)""?"
    dArr = Split(str, "}")
    For r = 0 To UBound(dArr)
        Set mats = .Execute(dArr(r))
        For Each mat In mats
            col = mat.submatches(0)
            If col < ub Then
                If mapID(col) > 0 Then
                    arr(r + 1, mapID(col)) = mat.submatches(1)
                End If
            End If
        Next
    Next
End With
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
Em chào Chị AutoReply xinh đẹp!
Em có tải file ở bài #7 về chạy mà nó không chạy. Em gửi Chị đường link nguồn một số trang lấy bảng giá chứng khoán:
https://priceboard.vcbs.com.vn/Priceboard
https://iboard.ssi.com.vn/bang-gia-ssi/hose
https://liveboard.cafef.vn/?center=1
https://banggia.vndirect.com.vn/chung-khoan/danh-muc
Chị có thể sửa lại code, chỉ cần bấm 1 cái là code chạy full các mã của 2 sàn giao dịch: Hồ chí minh và Hà Nội. Rất mong Chị giúp đỡ. Cảm ơn Chị nhiều!
 

File đính kèm

Lần chỉnh sửa cuối:
Trang vcbs

Mã:
Public Sub hello(ByVal selectedStocks As String, ByVal criteriaId As String, ByVal pthMktId As String)
Dim str As String, payload As String, arr
'template Payload:{"selectedStocks":"AAM,ABT","criteriaId":"-11","marketId":0,"lastSeq":0,"isReqTL":false,"isReqMK":false,"tlSymbol":"","pthMktId":""}
payload = "{""selectedStocks"":""" & selectedStocks & """,""criteriaId"":""" & criteriaId & """,""marketId"":0,""lastSeq"":0," & _
"""isReqTL"":false,""isReqMK"":false,""tlSymbol"":"""",""pthMktId"":""" & pthMktId & """}"
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", "http://quotes.vcbs.com.vn/PriceBoard/Acc/amw", False
    .setRequestHeader "Content-Type", "application/json"
    .send (payload)
    str = .ResponseText
End With
If Len(pthMktId) = 0 Then
    arr = tachBangGia(str)
Else
    arr = tachGDTT(str)
End If
Sheet1.Range("A5").Resize(UBound(arr) + 1000, UBound(arr, 2) + 50).ClearContents
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub

Private Function tachBangGia(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, arr, dArr, dicColName As Object, r As Long
Dim rowData, cellData, c As Long, colIndex As Long
Dim regec As Object, mats As Object, mat As Object
Set dicColName = CreateObject("Scripting.Dictionary")
dicColName.CompareMode = vbTextCompare
arr = Array("SB", "CL", "FL", "RE", "B3", "V3", "B2", "V2", "B1", "V1", "CH", "CP", "CV", "S1", _
"U1", "S2", "U2", "S3", "U3", "TT", "OP", "HI", "LO", "FB", "FS", "FR")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachBangGia = dArr
lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """f"":["))
lEnd = InStr(lStart, stResponse, "],")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 1), "\""", "")
arr = Split(stResponse, "Id:")

Set regec = CreateObject("VBScript.RegExp")
regec.Pattern = "[A-Z][A-Z0-9]\:[^,]*(,\d+)*"
'[A-Z][A-Z0-9]\:.*?(?=,[A-Z][A-Z0-9]\:)
regec.Global = True

For r = LBound(arr) + 1 To UBound(arr) Step 1
    Set mats = regec.Execute(arr(r))
    For Each mat In mats
        cellData = Split(mat, ":")
        If dicColName.exists(cellData(0)) Then
            colIndex = dicColName(cellData(0))
            dArr(r, colIndex) = IIf(CStr(cellData(1)) = "null", "", cellData(1))
        End If
    Next
Next
tachBangGia = dArr
End Function

Private Function tachGDTT(ByVal stResponse As String)
Dim lStart As Long, lEnd As Long, dicColName As Object, arr, dArr, r As Long
Dim colIndex As Long, rowData, colName As String, cellData, c As Long

Set dicColName = CreateObject("Scripting.Dictionary")
arr = Array("Symbol", "Price", "MatchVol", "fake", "Time")
For r = 0 To UBound(arr) Step 1
    dicColName(arr(r)) = r + 1
Next
ReDim dArr(1 To 1000, 1 To dicColName.Count)
tachGDTT = dArr

lStart = WorksheetFunction.Max(1, InStr(1, stResponse, """pm"":["))
lEnd = InStr(lStart, stResponse, "}]")
If lStart < 2 Or lEnd < 2 Then Exit Function
stResponse = Replace(Mid(stResponse, lStart, lEnd - lStart + 2), """", "")
arr = Split(stResponse, "{Id:")

For r = LBound(arr) + 1 To UBound(arr) Step 1
    rowData = Split(arr(r), ",")
    For c = LBound(rowData) + 1 To UBound(rowData) Step 1
        lStart = InStr(1, rowData(c), ":")
        If lStart > 0 Then
            cellData = Mid(rowData(c), lStart + 1)
            colName = Left(rowData(c), lStart - 1)
            If dicColName.exists(colName) Then
                colIndex = dicColName(colName)
                If colIndex = 2 Then
                    dArr(r, colIndex) = cellData / 1000
                    dArr(r, 4) = "=RC[-2] * RC[-1]"
                Else
                    dArr(r, colIndex) = cellData
                End If
            End If
        End If
    Next
Next
tachGDTT = dArr
End Function

Public Function createVcbsList()
Dim arr(1 To 50, 1 To 4), curRow As Long, str As String
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://quotes.vcbs.com.vn/PriceBoard", False
    .send
    str = .ResponseText
End With

tachGroup str, arr, "HOSE", curRow
tachGroup str, arr, "HNX", curRow
tachGroup str, arr, "UPCOM", curRow
createVcbsList = arr
End Function

Private Sub tachGroup(ByVal stResponse As String, ByRef arr, groupName As String, ByRef curRow As Long)
Dim lStart As Long, lEnd As Long, mats As Object, mat As Object
Static regec As Object, domDoc As Object
lStart = InStr(1, stResponse, "id=""" & groupName & "_Group""")
lEnd = InStr(lStart, stResponse, "</ul>")

If regec Is Nothing Then
    Set regec = CreateObject("VBScript.RegExp")
    regec.Pattern = "onclick\=\""selectTab\(([^\)]+)[\s\S]+?(<p>[\s\S]+?</p>)[\s\S]+?(value\=\""([\s\S]+?))?</li>"
    regec.IgnoreCase = True
    regec.Global = True
    Set domDoc = CreateObject("Msxml2.DOMDocument")
End If

Set mats = regec.Execute(Mid(stResponse, lStart, lEnd - lStart + 5))
For Each mat In mats
    curRow = curRow + 1
    arr(curRow, 1) = groupName
    domDoc.LoadXML mat.submatches(1)
    arr(curRow, 2) = domDoc.Text
    arr(curRow, 3) = mat.submatches(0)
    If Len(mat.submatches(3)) > 0 Then
        arr(curRow, 4) = Left(mat.submatches(3), InStr(1, mat.submatches(3), """") - 1)
    End If
Next
End Sub
Bạn AutoReply ơi bạn có thể sửa lại code của bảng VCBS được không vì hiện nó bị lỗi không dùng được.
Cảm ơn bạn nhiều!
 
Bạn AutoReply ơi bạn có thể sửa lại code của bảng VCBS được không vì hiện nó bị lỗi không dùng được.
Cảm ơn bạn nhiều!
Mình đã đổi lại link của VCBS nhưng nó bị lỗi ở câu lệnh bị bôi vàng như sau:
1.jpg
Bạn có thể sửa giúp được không? Cảm ơn bạn nhiều!
 
tren nay nhieu nguoi gioi qua, bai phuc bai phuc
 
trong file có cái nút bấm , tùy chọn sàn mà lấy nhé

Mã:
Public Sub hello(ByVal region As String, ByVal targetTB As String)
Dim arr(1 To 2000, 1 To 25), r As Long, str As String
Dim mats, mapID, mat, dArr, ub As Long, col As Long
With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "GET", "http://priceboard.fpts.com.vn/" & region & "/data.ashx?s=quote&l=" & targetTB, False
    .send
    str = .ResponseText
End With
mapID = Array(1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
        16, 17, 18, 19, -1, 20, 21, 22, 23, -1, 24, 25)
ub = UBound(mapID) + 1
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\[""?(\d{1,2})""?,""?([^\]""]+)""?"
    dArr = Split(str, "}")
    For r = 0 To UBound(dArr)
        Set mats = .Execute(dArr(r))
        For Each mat In mats
            col = mat.submatches(0)
            If col < ub Then
                If mapID(col) > 0 Then
                    arr(r + 1, mapID(col)) = mat.submatches(1)
                End If
            End If
        Next
    Next
End With
Sheet1.Range("A5").Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
Bạn có thể giúp mình chút được không vậy. Mình muốn lấy dữ liệu về excel để quản lý nó mà loay hoay mãi không biết sao. Loanh quanh Data from Web mãi mà ko đc ? Có thể zalo giúp mình với được ko vậy 0988 51 9889. Cám ơn bạn nhiều !
 

Bài viết mới nhất

Back
Top Bottom