标题: [问题求助] VBS获取网页源码乱码,求好心人帮忙! [打印本页]
作者: aab 时间: 2013-12-8 08:53 标题: VBS获取网页源码乱码,求好心人帮忙!
以下VBS脚本GET获取网页源代码,但是获取时,除了网页编码是GBK,其他的例如UTF-8/GB2312返回的中文都是乱码,在网上搜索了天半也没找到解决的办法,好心人帮下忙吧,谢谢了!- Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
- Call Msgbox(HttpGET("http://www.baidu.com/"))
-
- Function HttpGET(URL)
- Call Http.Open("GET", URL, False)
- Call Http.Send()
- HttpGET = Http.ResponseText()
- End Function
复制代码
作者: wankoilz 时间: 2013-12-8 09:47
这样行不行:- Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
- MsgBox HttpGET("http://www.baidu.com/")
-
- Function HttpGET(URL)
- Http.Open "GET", URL, False
- Http.Send()
- Http.ResponseText()
- With CreateObject("adodb.stream")
- .mode=3
- .type=2
- .charset="utf-8"
- .open
- .writeText(Http.ResponseText())
- .position=0
- .charset="gb2312"
- HttpGET=.readText()
- .close
- End With
- End Function
复制代码
作者: aab 时间: 2013-12-8 11:59
回复 2# wankoilz
测试了一下你改的代码,中文还是乱码,效果如下图:
作者: wscript 时间: 2013-12-8 12:13
应该用ResponseBody- Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
- MsgBox HttpGET("http://www.baidu.com/")
-
- Function HttpGET(URL)
- Http.Open "GET", URL, False
- Http.Send()
- Http.ResponseText()
- With CreateObject("adodb.stream")
- .mode=3
- .type=1
- .open
- .write Http.ResponseBody
- .position=0
- .type=2
- .charset="utf-8"
- HttpGET=.readText()
- .close
- End With
- End Function
复制代码
话说with这种写法真难看。。
作者: aab 时间: 2013-12-8 12:49
回复 4# wscript
多谢大家的帮忙,非常感谢你们,经过多次的测试和百度了你们给的写法,已经成功了,多谢2楼和4楼的兄弟!下面我把完整的代码也给大家分享一下!- Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
- Set Stream = CreateObject("Adodb.Stream")
- Call Msgbox(HttpGET("http://www.baidu.com/"))
-
- Function HttpGET(URL)
- Call Http.Open("GET", URL, False)
- Call Http.Send()
- Call Stream.Open()
- Call Stream.WriteText(Http.ResponseBody())
- Stream.Position = 0
- Stream.Charset = "UTF-8"
- Stream.Position = 2
- HttpGET = Stream.ReadText()
- End Function
复制代码
作者: wankoilz 时间: 2013-12-8 22:07
嗯,想起来了ResponseText()对utf-8支持不好!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |