2

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

这是一个网页responseText示例No 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源就像?????? ...

任何想法如何使它工作?

4

2 回答 2

3

对我有用的解决方案:

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

注意使用 ResponseBody 而不是 ResponseText

于 2017-05-20T13:32:03.707 回答
1

尝试使用 StrConv:

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

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

于 2013-03-03T15:33:00.953 回答