VBA WinHttp无映射字符
以下是我使用get HTML代码进行进一步处理的函数的源代码:VBA WinHttp无映射字符,vba,winhttp,winhttprequest,Vba,Winhttp,Winhttprequest,以下是我使用get HTML代码进行进一步处理的函数的源代码: Public Function DownloadTextFile(url As String) As String Dim oHTTP As WinHttp.WinHttpRequest Set oHTTP = New WinHttp.WinHttpRequest oHTTP.Open Method:="GET", url:=url, async:=False oHTTP.setRequestHea
Public Function DownloadTextFile(url As String) As String
Dim oHTTP As WinHttp.WinHttpRequest
Set oHTTP = New WinHttp.WinHttpRequest
oHTTP.Open Method:="GET", url:=url, async:=False
oHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
'oHTTP.setRequestHeader "Content-Type", "multipart/form-data; "
oHTTP.setRequestHeader "Content-Type", "text/html; charset=utf-8"
oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True
oHTTP.send
Dim success As Boolean
success = oHTTP.waitForResponse()
If Not success Then
Debug.Print "DOWNLOAD FAILED!"
Exit Function
End If
Dim responseText As String
Debug.Print oHTTP.responseText
responseText = oHTTP.responseText
'Set fs = CreateObject("Scripting.FileSystemObject")
'Set a = fs.CreateTextFile("c:\testfile.txt", True, False)
'Set a = fs.CreateTextFile("c:\testfile.txt", True, True)
'a.WriteLine oHTTP.responseText
'a.Close
Set oHTTP = Nothing
DownloadTextFile = responseText
End Function
它适用于大多数页面,但对于某些页面,responseText
是目标多字节代码页面中不存在Unicode字符的映射
以下是一个网页示例,responseText
为目标多字节代码页中不存在Unicode字符映射
这是一个无法编码的可疑字符(谷歌浏览器截图):
有时在同一个网站上,但对于不同的搜索结果,此函数不会生成错误,但是,immidate窗口中的HTML源代码类似于代码>
有什么办法让它工作吗?尝试使用StrConv:
DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode)
vbUnicode:使用系统的默认代码页将字符串转换为Unicode。(在Macintosh上不可用。)尝试使用StrConv:
DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode)
vbUnicode:使用系统的默认代码页将字符串转换为Unicode。(在Macintosh上不可用。)适合我的解决方案:
responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode)
请注意,使用ResponseBody而不是ResponseText对我有效的解决方案:
responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode)
请注意使用ResponseBody而不是ResponseTextHi Daniel。我已经检查了您的建议,它在解决问题方面提供了一些进展。对于具有屏幕截图中的字符的两个网页,调试程序将代码的“responseText=oHTTP.responseText”部分标记为,并且oHTTP.responseText包含“无映射字符…”,您是否尝试过:responseText=StrConv(oHTTP.responseText,vbUnicode)嗨,丹尼尔。我已经检查了您的建议,它在解决问题方面提供了一些进展。对于具有屏幕截图中的字符的两个网页,调试程序将代码的“responseText=oHTTP.responseText”部分标记为,并且oHTTP.responseText包含“无映射字符…”,您是否尝试过:responseText=StrConv(oHTTP.responseText,vbUnicode)?