gpt4 book ai didi

excel - xmlhttprequest 导致的 Unicode 字符

转载 作者:行者123 更新时间:2023-12-04 21:30:36 24 4
gpt4 key购买 nike

在这个 LINK >> @QHarr 引入了一种计算 Google 搜索结果的解决方案,这是代码

Public Sub GetResultsCount()
Dim sResponse As String, html As HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.google.com/search?q=mitsubishi", False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
Set html = New HTMLDocument
With html
.body.innerHTML = sResponse
Debug.Print .querySelector("#resultStats").innerText
End With

End Sub

代码可以正常工作..但是在即时窗口中我得到了这个无法理解的字符
enter image description here

如何修复它以显示在阿拉伯字符中?

感谢先进的帮助

最佳答案

我找到了这个帖子:VBA - Convert string to UNICODE ,并设法提出了一个解决方案(它适用于波兰语字符,不确定阿拉伯语)

Private Const CP_UTF8 = 65001

Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

Public Sub GetResultsCount()
Dim sResponse As String
Dim html As HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.google.com/search?q=mitsubishi", False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
Set html = New HTMLDocument

html.body.innerHTML = sResponse
Debug.Print sUTF8ToUni(StrConv(html.querySelector("#resultStats").innerText, vbFromUnicode))
End Sub

Public Function sUTF8ToUni(bySrc() As Byte) As String
' Converts a UTF-8 byte array to a Unicode string
Dim lBytes As Long, lNC As Long, lRet As Long

lBytes = UBound(bySrc) - LBound(bySrc) + 1
lNC = lBytes
sUTF8ToUni = String$(lNC, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bySrc(LBound(bySrc))), lBytes, StrPtr(sUTF8ToUni), lNC)
sUTF8ToUni = Left$(sUTF8ToUni, lRet)
End Function

关于excel - xmlhttprequest 导致的 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53123782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com