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

野比大雄1 wrote:
請問合計買賣超部分怎...(恕刪)


1369樓第2張圖的程式碼,就是方法,但資料請自行練習整理。

snare wrote:
1369樓第2張圖的...(恕刪)

您好我想抓的是 第三欄。謝謝您

1686 -2184 34 -465
野比大雄1 wrote:
您好我想抓的是 第三欄。






野比大雄1 wrote:
請問這資料怎麼抓取,...(恕刪)

您好:
我發現會沒有 -負號 再麻煩您謝謝
Sub c()
Dim URL As String, GetXml As Object


URL = "https://tw.stock.yahoo.com/quote/" & Sheets("stock").Cells(i, 1) & ".TW/broker-trading"

Set GetXml = CreateObject("WinHttp.WinHttpRequest.5.1")

With GetXml

.Open "GET", URL, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send

temp = Split(Split(.responsetext, """totalOversellVolK"":")(1), ",""tradeVolumeRate""")(0)
End With
Set GetXml = Nothing
Set Jsondata = Nothing

End Sub

snare
網頁原始碼中,本來就沒負號,是網頁根據totalOversellVolK這個格子,自動加上的,都寫主力"賣"超了,"賣"超不可能是正的吧?那一定是負值,請自己練習加程式碼補一個負號。
野比大雄1
snare 不好意思 我發現是我自己沒看清楚欄位名稱 已經處理好了 感謝提點 萬分感恩
請問哪邊寫錯有問題無法登入網頁
Sub 巨集2()
'
' 巨集2 巨集
'
Dim Http As Object
Dim Username As String, Password As String
Dim Url As String, Data As String

' 設定帳號和密碼
Username = "***@gmail.com"
Password = "***"

' 設定目標網頁的URL和POST資料
Url = "https://thefew.tw/login"
Data = "username=" & Username & "&password=" & Password

' 建立XMLHttpRequest物件
Set Http = CreateObject("MSXML2.XMLHTTP")

' 設定請求參數
Http.Open "POST", Url, False
Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

' 傳送請求
Http.send Data

' 檢查回應狀態碼,如果是200表示登入成功
If Http.Status = 200 Then
' 登入成功後,您可以使用XMLHttpRequest物件下載所需的資料
' ...
Else
MsgBox "登入失敗"
End If
' 設定帳號和密碼

With ActiveSheet.QueryTables.Add(Connection:="URL;https://thefew.tw/cb", _
Destination:=Range("$A$1"))
' .CommandType = 0
.Name = "cb"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Function
bank87012 wrote:
請問哪邊寫錯有問題無...(恕刪)


一般的網站才能用這種寫法

thefew 不行,因為您登入的地方不是thefew,是accounts.google
thefew => redirect => google => 登入 => 再回來 thefew =>用google登入thefew
所以您把帳號密碼send到thefew是沒用的

而且這之中,有超多的參數在交換、認證
不是您那短短幾行程式碼就能解決
建議另找免登入的資料來源
或是改用chromedriver selenium
bank87012
請問可以教學如何解嗎?
snare wrote:
到處都在下大雨,在家...(恕刪)
0 Then
Debug.Print Err.Description
End If

End Sub
Morten Hsu
很抱歉,操作錯誤。
Snare大師 您好:
出遊之前就參考#223樓將上櫃股票每日收盤價QT改成 XmlHttp,一個多月下來竟然覺得生疏了很多。測試的結果,測試工作表上竟然是空白,一筆資料都沒有。我也看不出所以然,請大師抽空提點一下,謝謝。完整程式碼如下:

Sub GettpexHtml()
'Html下載測試_上櫃股票每日收盤行情
Dim HTMLsourcecode As Object, Clipboard As Object, XmlHttp As Object
Dim Url As String, Urla As String, stock_id As String
Dim formattedDate As String ' 日期字串

Set HTMLsourcecode = CreateObject("htmlfile")
Set Clipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set XmlHttp = CreateObject("Msxml2.XMLHTTP")

On Error GoTo checkid

Application.ScreenUpdating = False
Sheets("測試工作表").Cells.Clear

'stock_id = InputBox("股票代碼", , "8050")
'If stock_id = "" Then Exit Sub

'ttt=timer

' 將西元年轉換成民國年並格式化成中文日期
Dim queryDate As Date
queryDate = Sheets("設定主畫面").Range("G2").Value '使用查詢日期

Dim chineseYear As Integer
chineseYear = Year(queryDate) - 1911 ' 轉換成民國年
formattedDate = chineseYear & Format(queryDate, "/mm/dd") ' 中文日期格式

' 顯示中文日期
MsgBox "formattedDate: " & formattedDate

' 修改 URL
'Url = "https://www.tpex.org.tw/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430_result.php?l=zh-tw&o=htm&d=113/01/10& "&se=EW&s=0,asc,0"
Urla = "https://www.tpex.org.tw/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430_result.php?l=zh-tw&o=htm&d=" & formattedDate & "&se=EW&s=0,asc,0"
Url = "https://www.tpex.org.tw/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430.php?l=zh-tw"

With XmlHttp
.Open "POST", Urla, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Referer", Url
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
‘.send "ajax=true&input_stock_code=" & stock_id
.send
HTMLsourcecode.body[removed] = .responseText
'Debug.Print HTMLsourcecode.body.innerhtml

End With

If HTMLsourcecode.getElementsByTagName("table").Length = 0 Then
Sheets("測試工作表").Range("A1") = "查無該筆資料,請重新查詢!!"
Exit Sub
End If

With Clipboard
.SetText HTMLsourcecode.body[removed]
.PutInClipboard
End With

With Sheets("測試工作表")
.Cells(1, 1).Select
.PasteSpecial Format:="HTML"
End With

Application.ScreenUpdating = True

Set Clipboard = Nothing
Set HTMLsourcecode = Nothing
Set XmlHttp = Nothing

checkid:

If Err.Number <> 0 Then
Debug.Print Err.Description
End If

End Sub
bank87012 wrote:
請問可以教學如何解嗎?


下載所有未整理前的原始資料,自己代公式算出那些登入才看到的數據
但那些公式是什麼,我不知道

這是其中一筆資料下載範例,網址換代碼(藍色)就可以下載其它筆(共340筆)
URL = "https://thefew.tw/api/prices/6836"



















代碼整理在附件



[點擊下載]
bank87012
語法輸入後出現某定義這個SUB或FUNCTION
Morten Hsu wrote:
測試工作表上竟然是空白


只寫重點,請自行比較那裡不同




文章分享
評分
評分
複製連結
請輸入您要前往的頁數(1 ~ 157)

今日熱門文章 網友點擊推薦!