我正在通过 VBA 使用 Microsoft.XMLHTTP 来提取网页的正文。这样做时,诸如 é 之类的字符会被替换为“?”或者同样没有用的东西。
这是基本代码:
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "GET", ThisWebPage, False
objHTTP.setRequestHeader "Content-Type", _
"application/x-www-form-urlencoded; charset=UTF-8"
objHTTP.Send ("")
strResponse = objHTTP.responseText
有什么方法可以完整地检索特殊字符的页面吗?
注意:
我也尝试使用此请求 header 但没有成功:
objHTTP.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
提前致谢。
解决方案
感谢 Ben.Vineyard(以及一些粗略的谷歌搜索),我能够使用以下代码提取重音字符:
' Create the XMLHTTP object
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
' Send the request
objHTTP.Open "GET", WhatWebPage, False
objHTTP.Send ("")
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
With BinaryStream
.Type = adTypeBinary
.Open
.Write objHTTP.ResponseBody
'Change stream type To binary
.Position = 0
.Type = adTypeText
'Specify charset For the source text (unicode) data.
.Charset = "iso-8859-1"
'Open the stream And get binary data from the object
strResponse = .ReadText
End With
最佳答案
问题可能是您实际上并未发送编码为 utf-8 的数据。它可能采用 Ansi 或您使用的任何字符串/文件编码。那么它将无法使用 ASCII 码中高于 127 的字符。你确定原始文本流是utf-8吗?您是否尝试过其他编码,例如 iso-* 格式之一?
关于vba - XMLHTTP 和特殊字符(例如重音符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7100229/