• 156

(不定期更新)使用VBA解決 excel web 查詢無法匯入、匯入太慢的股市資料

snare wrote:
token是變動的,(恕刪)


謝謝樓主快速提供解決方法;小弟也看了您列出的範例,但仍有個不解問題;在這個案例中,如何判斷token是變動的,並需經2次的網頁查詢,才能取得最後資料,能否簡略指導一下?
activer wrote:
如何判斷token是變動的,並需經2次的網頁查詢


打開瀏覽器進網頁=>查詢=>關掉瀏覽器=>進網頁=>查詢
可發現每次token值都不同,可知是變動的





傳回json的網址,需要send+key+參數
www.tcb-bank.com.tw/api/client/ForeignExchange/GetHistoryForeignExchange

所以要先在其它網頁取得key,通常是在查詢的前一頁取得key
www.tcb-bank.com.tw/personal-banking/deposit-exchange/exchange-rate/historical-rate

但不一定要前一頁,看網站設計,有些全網站用同一個
同網站其它頁面、首頁…等等,都可以取得參數
有些網站每頁都需要不同的key
snare wrote:
打開瀏覽器進網頁=>查詢=>關掉瀏覽器=>進網頁=>查詢
可發現每次token值都不同,可知是變動的


snare wrote:
token是變動的,(恕刪)

樓主您好,
小弟資質駑鈍,您在1200樓的程式碼中,有關token的取得,小弟以Debug.Print .responsetext方式,卻找不到有關token的字串(RequestVerificationToken"" type=""hidden"" value="""),請問如何將.responsetext,完整顯示,並找到token 的字串?


R_V_Token = Split(Split(.responsetext, """__RequestVerificationToken"" type=""hidden"" value=""")(1), """ />")(0)

Debug.Print .responsetext
activer wrote:
Debug.Print .responsetext方式,卻找不到有關token的字串


即時運算視窗
一、有字數限制,超過不顯示,也找不到
二、有編碼限制,有些正常內容會變亂碼

假設程式正常
(.responsetext) 的內容會等於(瀏覽器=>按滑鼠右鍵=>檢視網頁原始碼)
一般是在瀏覽器檢視網頁原始碼、f12開發者工具,做搜尋
有些人會用fiddler代替

如果要在excel中看,字數少、單純檢查用
debug.print .responsetext

字數多
cells(1,1)=.responsetext

字數更多,需切割字串,單一儲存格也有字數限制
alldata= Split(.responsetext, vbNewLine)
For Each temp In alldata
r = r + 1
Cells(r, 1) = temp
Next


(點我看大圖)
snare wrote:
字數更多,需切割字串,單一儲存格也有字數限制
alldata= Split(.responsetext, vbNewLine)(恕刪)


原來要檢視網頁原始碼,又上了一課;這段程式碼的結果,更是所需
snare您好:
敝人又來打擾您了,以下上網址也是JSON的格式,要如何直接找到關鍵的詞,匯入它的值呢?
主要想要找出"Shares Outstanding" ,滙入value以下primaryValue內的數值,如下圖。
因為美股是在下圖的位置,但在其他國家的股票時,就不在圖上的階層內。
美股是props->data->blocks->"9"->dataPoints->4->value這層,但其他國家在"9"的這階層有可能是7或8,所以是不是有可能直接到關鍵字後,滙入其值呢?
很感謝您撥空回應。

https://www.barrons.com/market-data/api/millstone?ticker=KO&PAGE={"renderTab":"company-people","assetType":"stock","analyticsValue":"stockcompany"}&fetchUrl=https://quote-barrons.millstone.mktw.dowjones.io/api/quote/profile?chartingSymbol=stock///KO&countrycode=

alfidpan wrote:
所以是不是有可能直接到關鍵字後,滙入其值呢?







一、
       On Error Resume Next
temp = Split(Split(.responsetext, "Shares Outstanding"",""value"":{")(1), "}},{")(0)
If temp = "" Then
Debug.Print "nothing"
Else
Debug.Print temp
End If


or

二、
      if  instr(.responsetext,"Shares Outstanding"",""value"":{")>0 then 
temp = Split(Split(.responsetext, "Shares Outstanding"",""value"":{")(1), "}},{")(0)
debug.print temp
else
debug.print "nothing"
end if

alfidpan

感謝snare,原來還有這一種簡單的方法,十分感謝。

2022-01-27 1:15
g80860

感謝板主,辛苦了.

2022-01-27 12:33
S大請問一下 我照著75樓方式做以 Url = "https://ws.api.cnyes.com/ws/api/v1/charting/history?resolution=D&symbol=TWS:2330:STOCK&from=1645142400&to=1640908800"e=1"去做可以帶得出資料,但是以 'Url = "https://ws.api.cnyes.com/ws/api/v1/charting/history?resolution=D&symbol=TWS:" & stock & ":STOCK&from=" & DataToUnixTime(endday) & "&to=" & DataToUnixTime(startday) & ""e=1"
Url_a = "https://invest.cnyes.com/twstock/TWS/" & stock & "/history#fixed"卻帶不出資料,請問是甚麼原因?


Sub Get_cnyes_Jsondata()

Dim Xmlhttp As Object, Jsondata As Object, Url As String, Url_a As String, stock As String, startday As String, endday As String, ttt As Double, DecodeJson, i As Integer
Set Jsondata = CreateObject("HtmlFile")
Set Xmlhttp = CreateObject("msxml2.xmlhttp")

Jsondata.write ""
'jsondata.write 這行是全形字,請自行改成半形,或直接用檔案中的程式碼

stock = InputBox("股票代號", , "2330") & ".TW"
startday = Format(InputBox("開始日期(8碼數字)", , Format(Date, "yyyymmdd") - 90), "####/##/##")
endday = Format(InputBox("結束日期(8碼數字)", , Format(Date, "yyyymmdd")), "####/##/##")



If startday > endday Or stock = "" Or startday = "" Or endday = "" Then
MsgBox "資料輸入錯誤", vbOKOnly, "請重新輸入"
Exit Sub
End If

ttt = Timer
Url = "https://ws.api.cnyes.com/ws/api/v1/charting/history?resolution=D&symbol=TWS:2330:STOCK&from=1645142400&to=1640908800"e=1"
'Url = "https://ws.api.cnyes.com/ws/api/v1/charting/history?resolution=D&symbol=TWS:" & stock & ":STOCK&from=" & DataToUnixTime(endday) & "&to=" & DataToUnixTime(startday) & ""e=1"
Url_a = "https://invest.cnyes.com/twstock/TWS/" & stock & "/history#fixed"

With Xmlhttp
.Open "GET", Url, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Referer", Url_a
.send
Set DecodeJson = CallByName(Jsondata.JsonParse(.responsetext), "data", VbGet)
End With

With Sheets("工作表1")
Application.ScreenUpdating = False

For i = 0 To CallByName(CallByName(DecodeJson, "t", VbGet), "length", VbGet) - 1

.Cells(i + 2, 1) = Format(CallByName(CallByName(DecodeJson, "t", VbGet), i, VbGet) / 86400 + #1/1/1970 8:00:00 AM#, "yyyy/mm/dd")
.Cells(i + 2, 2) = CallByName(CallByName(DecodeJson, "o", VbGet), i, VbGet)
.Cells(i + 2, 3) = CallByName(CallByName(DecodeJson, "h", VbGet), i, VbGet)
.Cells(i + 2, 4) = CallByName(CallByName(DecodeJson, "l", VbGet), i, VbGet)
.Cells(i + 2, 5) = CallByName(CallByName(DecodeJson, "c", VbGet), i, VbGet)
If i > 0 Then
.Cells(i + 2 - 1, 6) = .Cells(i + 2 - 1, 5) - .Cells(i + 2, 5)
.Cells(i + 2 - 1, 7) = Round((.Cells(i + 2 - 1, 6) / .Cells(i + 2 - 1, 5)) * 100, 2) & "%"
If .Cells(i + 2 - 1, 6) > 0 Then
.Cells(i + 2 - 1, 6).Font.Color = vbRed
.Cells(i + 2 - 1, 7).Font.Color = vbRed
End If
If .Cells(i + 2 - 1, 6) < 0 Then
.Cells(i + 2 - 1, 6).Font.Color = -11489280
.Cells(i + 2 - 1, 7).Font.Color = -11489280
End If
End If
.Cells(i + 2, 8) = CallByName(CallByName(DecodeJson, "v", VbGet), i, VbGet)

Next i

.Rows(i + 1).Clear
.Cells.EntireColumn.AutoFit
.Cells(1, 1).Select

Application.ScreenUpdating = True

End With

Debug.Print Timer - ttt

Set Xmlhttp = Nothing
Set DecodeJson = Nothing
Set Jsondata = Nothing

End Sub

't 日期
'o 高盤
'h 最高
'l 最低
'c 收盤
'v 成交張數
zxa23123 wrote:
但是以 'Url = "https://ws.api.cnyes.com/ws/api/v1/charting/history?resolution=D&symbol=TWS:" & stock & ":STOCK&from=" & DataToUnixTime(endday) & "&to=" & DataToUnixTime(startday) & ""e=1"
Url_a = "https://invest.cnyes.com/twstock/TWS/" & stock & "/history#fixed"卻帶不出資料,請問是甚麼原因?


您發問的文章內有很多字被切掉,假設您原始文章是正確的


錯誤有2
一、不需要.tw
stock = InputBox("股票代號", , "2330")


二、日期要用日期格式去加減,開始、結束,都要多一天
startday = Format(InputBox("開始日期(8碼數字)", , Format(Date - 91, "yyyymmdd")), "####/##/##")

endday = Format(InputBox("結束日期(8碼數字)", , Format(Date + 1, "yyyymmdd")), "####/##/##")



另外,漲跌(%)計算方式需修改
.Cells(i + 2 - 1, 7)=………這行請自行練習修改

zxa23123

感謝大師的回覆,問題確實如您所說,經修正後已經正常能使用了,謝謝您!

2022-02-18 9:51
  • 156
內文搜尋
X
評分
評分
複製連結
請輸入您要前往的頁數(1 ~ 156)
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?