【问题标题】:VBA WinHttp no mapping characterVBA WinHttp没有映射字符
【发布时间】:2013-02-17 15:33:36
【问题描述】:

这是我用来获取 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.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

它适用于大多数页面,但对于某些页面,responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

这是一个网页示例,其中responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

http://bzp0.portal.uzp.gov.pl/index.php?ogloszenie=browser&action=search&rodzajzamowienia=B&rodzajogloszenia=1&aktualne=1&datapublikacji_rodzaj=5&iloscwynikownastronie=20&offset=20

这是一个无法编码的可疑字符(来自谷歌浏览器的屏幕截图):

http://imageshack.us/photo/my-images/585/errsource.png/

有时在同一个网站,但对于不同的搜索结果,此功能不会产生错误,但是,即时窗口中的HTML源就像?????? ...

任何想法如何使它工作?

【问题讨论】:

    标签: vba winhttp winhttprequest


    【解决方案1】:

    对我有用的解决方案:

    responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode)
    

    注意使用 ResponseBody 而不是 ResponseText

    【讨论】:

      【解决方案2】:

      尝试使用StrConv:

      DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode)
      

      vbUnicode:使用系统的默认代码页将字符串转换为 Unicode。 (在 Macintosh 上不可用。)

      【讨论】:

      • 你好丹尼尔。我已经检查了您的建议,它提供了解决问题的一些进展。
      • 对于带有屏幕截图中字符的几个网页,调试器将代码的“responseText = oHTTP.responseText”部分标记为,并且oHTTP.responseText包含“无映射字符...”跨度>
      • 你试过了吗:responseText = StrConv(oHTTP.responseText,vbUnicode) ?
      猜你喜欢
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      相关资源
      最近更新 更多