这是我使用获取 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
示例No Mapping for the Unicode character exists in the target multi-byte code page
这是一个无法编码的可疑字符(来自谷歌浏览器的屏幕截图):
http://imageshack.us/photo/my-images/585/errsource.png/
有时在同一网站上但对于不同的搜索结果,此功能不会产生错误,但是,即时窗口中的HTML源就像?????? ...
任何想法如何使它工作?