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が であるWeb ページの例を次に示します。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

そして、これはエンコードできない疑わしい文字です (Google Chrome のスクリーンショット):

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

同じ Web サイトで時々検索結果が異なる場合、この関数はエラーを生成しませんが、その場合、即時ウィンドウの HTML ソースは次のようになります。?????? ...

それを機能させる方法はありますか?

4

2 に答える 2

1

StrConv を使用してみてください:

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

vbUnicode: システムの既定のコード ページを使用して文字列を Unicode に変換します。(マッキントッシュではご利用いただけません。)

于 2013-03-03T15:33:00.953 に答える