kadyysc5044 wrote:
但我的程式只能抓到標題第一列第一列資料如下:
最近成交價/漲跌價差/當盤成交量…
其它資料皆抓不到,可否看看我的程式是哪有問題,謝謝。...(恕刪)
傳回值是json,沒辦法用table方式處理
而且這個範例在2017年就寫過了
網址、代號、下載方式,請參考170樓、219樓 =>TSE_OTC() 副程式
170樓regexp就不要用了,難度太高,只是介紹可用的方法而已
請參考219樓、585樓、(其它樓)的,callbyname範例
JackLoh wrote:
我想那個是java的資料
不知樓主有没有解 ?...(恕刪)
是的,json格式,不能用table方式處理
方法請google "Delphi json 解析",範例很多,寫法也比vba簡單多了
因為解json是 delphi內建的基本功能啊
不會像vba google不到什麼有用的範例
您的問題只是回傳資料的url弄錯了
twse下載網址請參考下面的vba範例
也可參考170樓、219樓,自行增加網址中的代號,可自定義想要的資料
activer wrote:
您好,基本市況資料...(恕刪)
感謝幫忙
不過,剛剛試了一下,網頁有改版了,不需要send 2次,用msxml2.xmlhttp也正常,程式碼可以短一些了
無排版功能範例
'========================================================
Sub get_twse()
Dim Jsondata As Object, DecodeJson, url As String, url_a As String, getxml As Object, temp
Set Jsondata = CreateObject("HtmlFile")
Set getxml = CreateObject("msxml2.xmlhttp")
Jsondata.write ”<script>document.JsonParse=function (s) {return eval(’(’ + s + ’)’);}</script>”
'Jsondata.write 這行程式碼,符號是全形字,複製後,請改成半形字
url = "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_1101.tw|tse_1101B.tw|tse_1102.tw|tse_1103.tw|tse_1104.tw|tse_1108.tw|tse_1109.tw|tse_1110.tw|&json=1&delay=0&_="
url_a = "http://mis.twse.com.tw/stock/group.jsp?ind=01&ex=tse&currPage=0&type=all"
With getxml
.Open "GET", url & UNIXTime, False
.setRequestHeader "Referer", url_a
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
Set DecodeJson = Jsondata.JsonParse(.responsetext)
Set temp = CallByName(DecodeJson, "msgArray", VbGet)
Debug.Print CallByName(temp, "0", VbGet).a
Debug.Print CallByName(temp, "0", VbGet).b
Debug.Print CallByName(CallByName(temp, "0", VbGet), "z", VbGet)
' callbyname 後面的 .代號 有分大小寫,z 會變大寫,所以不能用.z代替,需直接使用callbyname
'而且z不一定會出現,程式碼中,需另外加判斷,避免出錯,程式碼中斷
'其它會變大寫或消失的代號,也一樣方法
End With
Set DecodeJson = Nothing
Set Jsondata = Nothing
Set getxml = Nothing
set temp=Nothing
End Sub
Function UNIXTime()
UNIXTime = Round(((Date - #1/1/1970#) * 86400 + Timer) * 1000, 0)
End Function
'========================================================