剛看了您的文章,好強,好像什麼都能抓,在此跟您請教一個問題,
https://mops.twse.com.tw/mops/#/web/t163sb04
公開資訊觀測站=>匯總報表=>財務報表=>綜合損益表,改版後無法抓取,程式碼該如何修改解決?
我的程式碼如下:
Set HTMLcode = CreateObject("htmlfile")
Set HttpReq = CreateObject("msxml2.xmlhttp")
URL = "https://mops.twse.com.tw/mops/web/ajax_t163sb04"
For i = 1 To Sdowntime
Application.StatusBar = GetProgress("損益表下載", i, Sdowntime)
Y = Sheets("新損益表").Cells(1, firstSeasonColumn + i - 1) - 1911
Q = Sheets("新損益表").Cells(2, firstSeasonColumn + i - 1)
With Sheets("暫存")
For k = 1 To 2 ' sii、otc共兩次
If k = 1 Then
TYPEK = "sii" '上市
Else
TYPEK = "otc" '上櫃
End If
.Cells.Clear
Call waitSecond(1)
With HttpReq
.Open "POST", URL, False
.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.SetRequestHeader "Cache-Control", "no-cache"
.SetRequestHeader "Pragma", "no-cache"
.SetRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
'.Send "encodeURIComponent=1&step=1&firstin=1&off=1&TYPEK=" & sii & "&year=" & Y & "&season=" & Q
.Send "encodeURIComponent=1&step=1&firstin=1&off=1&isQuery=Y&TYPEK=" & TYPEK & "&year=" & Y & "&season=0" & Q
'encodeURIComponent=1&step=1&firstin=1&off=1&isQuery=Y&TYPEK=sii&year=106&season=01'上市
'encodeURIComponent=1&step=1&firstin=1&off=1&isQuery=Y&TYPEK=otc&year=106&season=01'上櫃
Do Until .readyState = 4: DoEvents: Loop
HTMLcode.Body[removed] = .ResponseText
End With
lastrow = 0
For TableCnt = 1 To HTMLcode.all.tags("table").Length - 1 '從1開始,去標頭
Set Table = HTMLcode.all.tags("table")(TableCnt).Rows
For s = 0 To Table.Length - 1
For t = 0 To Table(s).Cells.Length - 1
.Cells(s + 1 + lastrow, t + 1) = Table(s).Cells(t).innerText
Next
Next
lastrow = lastrow + s + 3
Next
