以下是我使用的函数的来源获取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/
在同一网站上不时但对于不同的搜索结果,此函数不会产生错误,但是,在immidiate窗口中的HTML源代码就像?????? ...
任何想法如何使其发挥作用?
答案 0 :(得分:2)
对我有用的解决方案:
responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode)
请注意使用ResponseBody而不是ResponseText
答案 1 :(得分:1)
尝试使用StrConv:
DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode)
vbUnicode:使用系统的默认代码页将字符串转换为Unicode。 (在Macintosh上不可用。)