これは、さらに処理するために 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
ほとんどのページで機能しますが、一部のページでresponseText
はNo 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
そして、これはエンコードできない疑わしい文字です (Google Chrome のスクリーンショット):
http://imageshack.us/photo/my-images/585/errsource.png/
同じ Web サイトで時々検索結果が異なる場合、この関数はエラーを生成しませんが、その場合、即時ウィンドウの HTML ソースは次のようになります。?????? ...
それを機能させる方法はありますか?