• 156

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

snare wrote:
(20190930 20...(恕刪)


好的 感謝
請問之前自己用vba下載上市、上櫃盤後csv檔。之前上市股票的部份本來因為證交所改版不能用了,有幸在此看到樓主的解決方式,又可以自動下載了,在此感謝。

前陣子上櫃盤後的csv檔無法自動下載了,問題和當初上市一樣vba除錯都停在黃色底色的地方,想請問不知是哪裡出問題了。感謝。
victor0726 wrote:
前陣子上櫃盤後的csv檔無法自動下載了...(恕刪)


https
樓主您好:
我試著將其改成https,結果還是一樣。但我把
Set HttpReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
改成
Set HttpReq = CreateObject("Msxml2.XMLHTTP")
就成功了。

不知道差別在哪裡。 非常感謝您。
victor0726 wrote:
不知道差別在哪裡。 非常感謝您。...(恕刪)


功能不同,詳細請參考

英文
https://blogs.msdn.microsoft.com/xmlteam/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer/
https://support.microsoft.com/zh-tw/help/290761/frequently-asked-questions-about-serverxmlhttp
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms763742(v=vs.85)
https://support.microsoft.com/en-us/help/289481/you-may-need-to-run-the-proxycfg-tool-for-serverxmlhttp-to-work
https://support.microsoft.com/en-us/help/290761/frequently-asked-questions-about-serverxmlhttp

中文
https://developer.mozilla.org/zh-TW/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
https://developer.mozilla.org/zh-TW/docs/Web/Guide/AJAX/Getting_Started
Snare 大 : 你好!

306樓的 Wantgoo.xlsm 檔 最近執行 顯現"安全通道支援錯誤" 不知網站是改變問題 ? 請Snare 大能否抽空
指導要如何修正 解決 謝謝!
alantsai5840 wrote:
306樓的 Wantgoo.xlsm 檔 最近執行 顯現"安全通道支援錯誤" 不知網站是改變問題 ? (恕刪)


Set Xmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
改成
Set Xmlhttp = CreateObject("Msxml2.XMLHTTP")

object詳細功能,請參考695樓
Snare 大 : 你好!
謝謝! 抽空指導!
snare大大你好!

請問我要用VBA下載此網站(https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_05.php?l=zh-tw)的EXCEL檔,但是發現他的每月份下載網址比如108/11參數DOC_ID=2118,DOC_ID每月的值沒有一定的規則性,我要如何去取得DOC_ID然後下載(https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_dl.php?DOC_ID=2118),另外檢查網頁原始碼,也沒有發現對應的table在哪裡。

謝謝
peter624 wrote:
(https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_dl.php?DOC_ID=2118),另外檢查網頁原始碼,也沒有發現對應的table在哪裡。
(恕刪)

因為沒table,表格內的資料是json格式,用程式整理的,所以xmlhttp找不到table
除非您改用ie object方式,才能用table




peter624 wrote:
請問我要用VBA下載此網站(https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_05.php?l=zh-tw)的EXCEL檔,但是發現他的每月份下載網址比如108/11參數DOC_ID=2118,DOC_ID每月的值沒有一定的規則性,我要如何去取得DOC_ID然後下載(恕刪)


看起來好像有規則,又好像沒有,請自行研究





使用開發者工具追蹤可知,表格內的資料是由這個網址產生
https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_result.php?l=zh-tw&t=5&sd=108/01&ed=108/12/01&_=1575932601640


'取得下載網址範例
Sub Get_tpex()

Dim Xmlhttp As Object, Jsondata As Object, DecodeJson, temp
Dim Url As String, Url_a As String, Url_b As String, sd As String, ed As String, i As Integer

Set Jsondata = CreateObject("HtmlFile")
Jsondata.write ”<script>document.JsonParse=function (s) {return eval(’(’ + s + ’)’);}</script>”
'jsondata.write 這行符號是全形字,請自行改成半形,或回頭找其它檔案中的程式碼

Sheets("工作表1").Cells.Clear

sd = "99/02"
ed = "108/12"

Url = "https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_result.php?l=zh-tw&t=5&sd=" & sd & "&ed=" & ed & "&_="
Url_a = "https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_05.php?l=zh-tw"
Url_b = "https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_dl.php?DOC_ID="

Set Xmlhttp = CreateObject("Msxml2.XMLHTTP")

With Xmlhttp

.Open "GET", Url & UNIXTime, False
.setRequestHeader "Referer", Url_a
.send

Set DecodeJson = Jsondata.JsonParse(.responsetext)
Set temp = CallByName(DecodeJson, "aaData", VbGet)

For i = 0 To CallByName(temp, "length", VbGet) - 1
Cells(i + 1, 1) = CallByName(CallByName(temp, i, VbGet), "0", VbGet)
Cells(i + 1, 2) = Url_b & CallByName(CallByName(temp, i, VbGet), "1", VbGet)
Next i

End With

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

End Sub

Function UNIXTime()

UNIXTime = Round(((Date - #1/1/1970#) * 86400 + Timer) * 1000, 0)

End Function



'下載檔案範例
'部份程式碼,請參考269樓、271樓,自行加入
Sub download()

Dim Xmlhttp As Object
Dim Url As String, Url_a As String, FileName As String

Url = "https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_dl.php?DOC_ID=2118"
Url_a = "https://www.tpex.org.tw/web/stock/statistics/monthly/monthly_rpt_mkt_info_05.php?l=zh-tw"

Set Xmlhttp = CreateObject("Msxml2.XMLHTTP")

With Xmlhttp
.Open "GET", Url, False
.setRequestHeader "Referer", Url_a
.send

'取檔名程式碼
End With

'存檔程式碼

'開檔程式碼,請自行錄巨集使用workbooks.open,或建立CreateObject("Excel.Application")


Set Xmlhttp = Nothing

End Sub


在知道正確網址的情況下,下載方式也可以改用urlmon函式庫,一行程式碼就能下載


'部份程式碼,請參考61樓,自行加入
'64位元excel ,Private Declare 需改成 Private Declare PtrSafe
Sub test()
URLDownloadToFile 0, "網址", "完整路徑+自訂檔名(自訂副檔名)", 0, 0
End Sub
  • 156
內文搜尋
X
評分
評分
複製連結
請輸入您要前往的頁數(1 ~ 156)
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?