vb 获取网页源码后乱码,咋回事呢
PrivateFunctionHtmlStr$(Url$)'提取网页源码函数DimXmlHttpSetXmlHttp=CreateObject("Microsoft.XM...
Private Function HtmlStr$(Url$) '提取网页源码函数
Dim XmlHttp
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", Url, False
XmlHttp.Send
If XmlHttp.ReadyState = 4 Then HtmlStr = StrConv(XmlHttp.ResponseBody, vbUnicode)
Set XmlHttp = Nothing
End Function
Private Sub Command1_Click()
Dim t1 As String
If InStr(1, HtmlStr$(Text1.Text), "charset=utf-8") > 0 Then
Text2.Text = UTF8_Decode(HtmlStr$(Text1.Text))
Else
Text2.Text = HtmlStr$(Text1.Text)
End If
网址是新华网的新闻
问题是不转换完全是乱码,转换后,还有乱码,像下面这样
"中新社华盛顿9?2日电(记?德永?美国海军22日宣布" 展开
Dim XmlHttp
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", Url, False
XmlHttp.Send
If XmlHttp.ReadyState = 4 Then HtmlStr = StrConv(XmlHttp.ResponseBody, vbUnicode)
Set XmlHttp = Nothing
End Function
Private Sub Command1_Click()
Dim t1 As String
If InStr(1, HtmlStr$(Text1.Text), "charset=utf-8") > 0 Then
Text2.Text = UTF8_Decode(HtmlStr$(Text1.Text))
Else
Text2.Text = HtmlStr$(Text1.Text)
End If
网址是新华网的新闻
问题是不转换完全是乱码,转换后,还有乱码,像下面这样
"中新社华盛顿9?2日电(记?德永?美国海军22日宣布" 展开
3个回答
展开全部
utf-8 编码问题
用我这个试试
Public Function GetBody(ByVal URL$, Optional ByVal Coding$ = "GB2312")
Dim ObjXML
On Error Resume Next
Set ObjXML = CreateObject("Microsoft.XMLHTTP")
With ObjXML
.Open "Get", URL, False, "", ""
.setRequestHeader "If-Modified-Since", "0"
.Send
GetBody = .responseBody
End With
GetBody = BytesToBstr(GetBody, Coding)
Set ObjXML = Nothing
End Function
Public Function BytesToBstr(strBody, CodeBase)
Dim ObjStream
Set ObjStream = CreateObject("Adodb.Stream")
With ObjStream
.Type = 1
.Mode = 3
.Open
.Write strBody
.Position = 0
.Type = 2
.Charset = CodeBase
BytesToBstr = .ReadText
.Close
End With
Set ObjStream = Nothing
End Function
注意第一行代码中Coding$ = "GB2312" 表示获取的网页内容为GB2312编码格式,如果出现乱码,那么就换成Coding$ = "utf-8"
返回值为:
a = GetBody("http://www.baidu.com")
用我这个试试
Public Function GetBody(ByVal URL$, Optional ByVal Coding$ = "GB2312")
Dim ObjXML
On Error Resume Next
Set ObjXML = CreateObject("Microsoft.XMLHTTP")
With ObjXML
.Open "Get", URL, False, "", ""
.setRequestHeader "If-Modified-Since", "0"
.Send
GetBody = .responseBody
End With
GetBody = BytesToBstr(GetBody, Coding)
Set ObjXML = Nothing
End Function
Public Function BytesToBstr(strBody, CodeBase)
Dim ObjStream
Set ObjStream = CreateObject("Adodb.Stream")
With ObjStream
.Type = 1
.Mode = 3
.Open
.Write strBody
.Position = 0
.Type = 2
.Charset = CodeBase
BytesToBstr = .ReadText
.Close
End With
Set ObjStream = Nothing
End Function
注意第一行代码中Coding$ = "GB2312" 表示获取的网页内容为GB2312编码格式,如果出现乱码,那么就换成Coding$ = "utf-8"
返回值为:
a = GetBody("http://www.baidu.com")
2011-10-13
展开全部
78ADBA
您 好 :
强 烈 建 议 你 直 接 看 一 下 这 个 吧 ! 可 以 节 省 你 很 多 时 间 !
aeh www.ZybF.sumwu.com。pla
㊣∩○◎◇→←〓△→
引用microsoft activex object data 2.5
Private Sub Form_Load()
Dim xml As Object, ReturnType As String
Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "", False
xml.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
xml.Send
While Not xml.readyState = 4
DoEvents
Wend
Dim intFile As Integer
intFile = FreeFile
Open "c:\1.html" For Binary Access Write As #intFile
Put #intFile, , xml.responsebody
Close #intFile
Dim s As New Stream
s.Open
s.Type = adTypeText
s.Charset = "shift-jis"
s.Position = 0
s.LoadFromFile "c:\1.html"
s.Position = 0
Debug.Print s.ReadText()
End Sub
确实多了几个,你查找<html,把前面的字符去掉
您 好 :
强 烈 建 议 你 直 接 看 一 下 这 个 吧 ! 可 以 节 省 你 很 多 时 间 !
aeh www.ZybF.sumwu.com。pla
㊣∩○◎◇→←〓△→
引用microsoft activex object data 2.5
Private Sub Form_Load()
Dim xml As Object, ReturnType As String
Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "", False
xml.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
xml.Send
While Not xml.readyState = 4
DoEvents
Wend
Dim intFile As Integer
intFile = FreeFile
Open "c:\1.html" For Binary Access Write As #intFile
Put #intFile, , xml.responsebody
Close #intFile
Dim s As New Stream
s.Open
s.Type = adTypeText
s.Charset = "shift-jis"
s.Position = 0
s.LoadFromFile "c:\1.html"
s.Position = 0
Debug.Print s.ReadText()
End Sub
确实多了几个,你查找<html,把前面的字符去掉
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网页错误吧 试试刷新
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询