问题描述
我使用Microsoft.XMLHTTP通过VBA拉入网页的正文。在这样做时,诸如é的字符被替换为?
以下是基本代码:
设置objHTTP = CreateObject(Microsoft.XMLHTTP)
/ pre>
objHTTP.OpenGET,ThisWebPage,False
objHTTP.setRequestHeaderContent-Type,_
application / x-www-form-urlencoded; charset = UTF-8
objHTTP.Send()
strResponse = objHTTP.responseText
是否有任何方法可以检索包含特殊字符的页面?
注意:
我也试过使用这个请求头没有成功:
objHTTP.setRequestHeaderContent-Type,content = text / html; charset = iso-8859-1
由于Ben.Vineyard(和一些粗略的Google搜索),我可以使用以下代码拉取重音字符:'创建XMLHTTP对象
设置objHTTP = CreateObject(Microsoft.XMLHTTP)
'发送请求
objHTTP.OpenGET,WhatWebPage ,False
objHTTP.Send()
Dim BinaryStream
Set BinaryStream = CreateObject(ADODB.Stream)
With BinaryStream
.Type = adTypeBinary
.Open
.Write objHTTP.ResponseBody
'将流类型更改为二进制
.Position = 0
.Type = adTypeText
'指定charset用于源文本(unicode)数据。
.Charset =iso-8859-1
'打开流并从对象获取二进制数据
strResponse = .ReadText
结束于
解决方案问题可能是你实际上并不发送编码为utf-8。它可能在Ansi或任何字符串/文件编码使用。然后它将不能在ASCII码中使用高于127的字符。您确定原始文本流是utf-8吗?你是否尝试过像其他iso- *格式的其他编码?
I am using Microsoft.XMLHTTP via VBA to pull in the body of a web page. In doing so, characters such as é get replaced with "?" or something equally not useful.
Here's the basic code:
Set objHTTP = CreateObject("Microsoft.XMLHTTP") objHTTP.Open "GET", ThisWebPage, False objHTTP.setRequestHeader "Content-Type", _ "application/x-www-form-urlencoded; charset=UTF-8" objHTTP.Send ("") strResponse = objHTTP.responseText
Is there any way to retrieve the page with the special characters intact?
Note:I have also tried using this request header with no success:
objHTTP.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
Thanks in advance.
Solution
Thanks to Ben.Vineyard (and some cursory Googling), I'm able to pull accented characters with the following code:' Create the XMLHTTP object Set objHTTP = CreateObject("Microsoft.XMLHTTP") ' Send the request objHTTP.Open "GET", WhatWebPage, False objHTTP.Send ("") Dim BinaryStream Set BinaryStream = CreateObject("ADODB.Stream") With BinaryStream .Type = adTypeBinary .Open .Write objHTTP.ResponseBody 'Change stream type To binary .Position = 0 .Type = adTypeText 'Specify charset For the source text (unicode) data. .Charset = "iso-8859-1" 'Open the stream And get binary data from the object strResponse = .ReadText End With
解决方案The problem could be that you do not actually send the data encoded as utf-8. It might be in Ansi or whatever string/file encoding you use. And then it will not be able to use characters high than 127 in the ASCII code. Are you sure that the original text stream is utf-8? Have you tried other encoding like one of the iso-* formats?
这篇关于XMLHTTP和特殊字符(例如,重音符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!