標題:
[發問]
由網頁取得的資料,不知道要如何同步編碼成正確之中文碼回傳
[打印本頁]
作者:
c_c_lai
時間:
2016-6-15 11:50
標題:
由網頁取得的資料,不知道要如何同步編碼成正確之中文碼回傳
本帖最後由 c_c_lai 於 2016-6-15 15:21 編輯
請問各位大大,
由網頁取得的資料,不知道要如何同步編碼成正確之中文碼回傳,
如附圖 (上圖) :
[attach]24493[/attach]
正確應為 (下圖) :
[attach]24494[/attach]
Sub 上市當沖4()
Dim xTable As Object, k As Integer, c As Integer, R As Integer ' , sn As Integer
Dim url As String, cts As Integer, E As Variant, xDate As String ' , rc As Integer
Dim oXmlhttp As Object, oHtmldoc As Object, select2 As String ' , tm
Dim TVal() As Variant, sPost As String
If Select_Name = -1 Then Exit Sub
TVal = Array("MS", "", "0049", "0099P", "019919T", "0999", "0999P", "01", "02", "03", _
"04", "05", "06", "07", "21", "22", "08", "09", "10", _
"11", "12", "13", "24", "25", "26", "27", "28", "29", _
"30", "31", "14", "15", "16", "17", "18", "23", "9299", "19", "20", "CB")
url = "http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php"
xDate = Format(Sheets("總表").[B1], "EE/MM/DD")
sPost = "input_date=" & Replace(xDate, "/", "%2F") & "&select2=" & TVal(Select_Name) 'urlencode
Set oXmlhttp = CreateObject("msxml2.xmlhttp")
Set oHtmldoc = CreateObject("htmlfile")
With Sheets("上市")
.Select
.Cells.Clear
With oXmlhttp
.Open "Post", url, False
' .setRequestHeader "Connection", "Keep-Alive" ' 短時間內多次查詢建議可加這行
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(sPost)
.Send sPost
' 上面 Open 參數用 False (=同步),可以不用再判斷 status
' Do While .Status <> 200 Or .readyState <> 4: DoEvents: Loop
oHtmldoc.write .responseText
' MsgBox .responseText
End With
Set xTable = oHtmldoc.all.tags("TABLE")
' Stop
For Each E In Array(8, 10) ' 8, 10 -> "TABLE"
Set xTable = oHtmldoc.all.tags("TABLE")(E)
' Set xTable = oHtmldoc.all.tags("TABLE")(0)
k = k + 1
For R = 0 To xTable.Rows.Length - 1
For c = 0 To xTable.Rows(R).Cells.Length - 1
Sheets("上市").Cells(k, c + 1) = xTable.Rows(R).Cells(c).INNERTEXT
Next
k = k + 1
Next
If Right(sPost, 3) <> "t2=" Then Exit For
Next
End With
End Sub
複製代碼
Private Function Select_Name() As Integer
With Sheets("總表").ComboBox1
If .ListIndex = -1 Then MsgBox ("您尚未選擇「產業類別」,請於" & vbCrLf & "確認後再次點選『開啟網頁』," & vbCrLf & "謝謝您!")
Select_Name = .ListIndex ' Select_Name = -1,0,1,2,3,4,5,6,7,8,9,.....39
End With
End Function
複製代碼
謝謝各位大大!
作者:
c_c_lai
時間:
2016-6-15 15:16
本帖最後由 c_c_lai 於 2016-6-15 15:22 編輯
對不起,大概我沒描述清楚。樓上的圖片均是透過同樣的網址 ,
而以不同連結處理方式執行出來的結果。
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php", Destination:=Range( _
"$A$1"))
複製代碼
連結處理方式 (下圖) 則是正常;但是以
With oXmlhttp
.Open "Post", url, False
' .setRequestHeader "Connection", "Keep-Alive" ' 短時間內多次查詢建議可加這行
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Content-Length", Len(sPost)
.Send sPost
' 上面 Open 參數用 False (=同步),可以不用再判斷 status
' Do While .Status <> 200 Or .readyState <> 4: DoEvents: Loop
oHtmldoc.write .responseText
' MsgBox .responseText
End With
複製代碼
(上圖) 卻是亂碼。
附上檔案堤供各位大大測試即知,謝謝囉!
[attach]24500[/attach]
作者:
c_c_lai
時間:
2016-6-15 17:02
加入可正常會之程式模組,供作比對:
[attach]24503[/attach]
[attach]24504[/attach]
作者:
stillfish00
時間:
2016-6-15 18:37
回復
3#
c_c_lai
可查查 ADODB.Stream 用法 ,
二進位方式寫入 .responseBody , 再指定字符集以 "big5" 讀出來
作者:
c_c_lai
時間:
2016-6-15 19:00
回復
4#
stillfish00
不好意思,試了一會還是沒有掌握到
ADODB.Stream 用法心得 , 能否就以我的
test2.rar 為題解惑呢?
謝謝你囉!
作者:
joey0415
時間:
2016-6-16 08:59
回復
5#
c_c_lai
賴大試試,我只會拿以前的來改
[attach]24512[/attach]
Sub 證交所()
Dim strText As String
Dim i As Integer
Dim j As Integer
Dim nRow As Integer
Dim xRow As Integer
Dim nCol As Byte
Dim TR As Object
Dim TD As Object
Dim Arr()
Cells.Clear
With CreateObject("winhttp.winhttprequest.5.1")
.Open "POST", "http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php", False
.setrequestheader "Referer", "http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php"
.setrequestheader "Content-Type", "application/x-www-form-urlencoded"
.Send "input_date=105%2F06%2F15&select2=21&login_btn=+%ACd%B8%DF+"
strText = BinToStr(.ResponseBody, "BIG5") '要注意網頁編碼
End With
With CreateObject("htmlfile")
.Write strText
Set hTable = .all.tags("table")(8)
tt = hTable.Rows.Length
With ActiveSheet
For i = 0 To hTable.Rows.Length - 1
For j = 0 To hTable.Rows(i).Cells.Length - 1
.Cells(i + 1, j + 1) = hTable.Rows(i).Cells(j).innertext
Next
Next
End With
End With
End Sub
Function BinToStr(arrBin, strChrs)
With CreateObject("ADODB.Stream")
.Type = 2
.Open
.Writetext arrBin
.Position = 0
.Charset = strChrs
.Position = 2
BinToStr = .ReadText
.Close
End With
End Function
複製代碼
作者:
c_c_lai
時間:
2016-6-16 11:26
本帖最後由 c_c_lai 於 2016-6-16 11:44 編輯
回復
6#
joey0415
回復
4#
stillfish00
非常感謝兩位大大的不吝指導,終告完成了。
真是臨門一竅,不點不醒。joey0415 大大提供的範例
以及增加了 Function BinToStr(arrBin, strChrs) 的函式
它扮演了一個非常重要的腳色,受教了!
[attach]24513[/attach]
作者:
joey0415
時間:
2016-6-16 12:20
回復
7#
c_c_lai
賴大以前幫助小弟很多,小弟只把以前抄的東西修改一下讓賴大試試
作者:
yoyobuy
時間:
2016-6-16 12:42
回復
6#
joey0415
請問這個用你的 BinToStr( ) 為何就無法轉成功呢
Sub Test()
Dim xTable As Object, k As Integer, C As Integer, R As Integer ' , sn As Integer
Dim url As String, cts As Integer, E As Variant, xDate As String ' , rc As Integer
Dim oXmlhttp As Object, oHtmldoc As Object, select2 As String ' , tm
xDate = "105/06/13"
url = "http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php"
sPost = "input_date=" & Replace(xDate, "/", "%2F") & "&select2=" & "01" 'urlencode
Set oXmlhttp = CreateObject("msxml2.xmlhttp")
Set oHtmldoc = CreateObject("htmlfile")
With oXmlhttp
.Open "Post", url, False
'.setRequestHeader "Connection", "Keep-Alive" '短時間內多次查詢建議可加這行
.setRequestHeader "Content-Type", "text/html"
.setRequestHeader "Content-Length", Len(sPost)
.Send sPost
oHtmldoc.Write .responseText
End With
Set xTable = oHtmldoc.ALL.tags("TABLE")
' Stop
' 看看區域變數視窗 xTable 的內容
Set xTable = oHtmldoc.ALL.tags("TABLE")(0)
' Stop
' 再次看看區域變數視窗 xTable 的內容
'MsgBox xTable.INNERTEXT
'Debug.Print xTable.INNERTEXT
gg = BinToStr(xTable.INNERTEXT, "BIG5")
Debug.Print gg
End Sub
Function BinToStr(arrBin, strChrs)
Set objstream = CreateObject("ADODB.Stream")
With objstream
.Type = 2
.Open
.WriteText arrBin
.Position = 0
.Charset = strChrs
.Position = 2
BinToStr = .ReadText
.Close
End With
End Function
作者:
c_c_lai
時間:
2016-6-16 13:05
回復
9#
yoyobuy
請仔細看一下 #7 圖片裡的下註,便會明瞭了。
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)