VB获取网页源代码的五种方法

小歆14年前软件源码06308
方法1:inet控件调用方法 Inet1.OpenURL
    添加microsoft ineternet transfor conctrol6.0 控件
                              

方法2:XMLHTTP

'如果出现乱码,UTF-8可改为GB2312
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

Private Sub Command1_Click()
u = "http://wenku.baidu.com/search?word=vb&lm=0&od=0&fr=top_search"
Text1.Text = GetBody(u) '最好用richbox富文本框
End Sub


方法3:WinHttp

Private Sub Command4_Click()
   Set IEread = CreateObject("WinHttp.WinHttpRequest.5.1")
   CallByName IEread, "Open", VbMethod, "GET", URLaddr, True
   CallByName IEread, "Send", VbMethod
   CallByName IEread, "WaitForResponse", VbMethod
   aa = CallByName(IEread, "ResponseText", VbMethod)
   RichTextBox1.Text = aa
End Sub


方法4:'利用WebBrowser控件

Private Sub Command1_Click()
Dim strContent As String, i As Integer
Text1 = ""
For i = 0 To WebBrowser1.Document.All.length - 1
    If WebBrowser1.Document.All(i).tagName = "HTML" Then
        strContent = strContent & WebBrowser1.Document.All(i).innerHTML
    Exit For
End If
Next i
Text1 = strContent
End Sub


方法5:利用Inet控件的Execute方法

Private Sub Command5_Click()
Text1 = ""
Inet1.Execute Text2.Text, "GET"
While Inet1.StillExecuting
DoEvents
Wend
Text1.Text = Inet1.GetChunk(Len(Text1.Text))
End Sub



相关文章

实现VB与EXCEL的无缝连接

  VB是常用的应用软件开发工具之一,由于VB的报表功能有限,而且一但报表格式发生变化,就得相应修改程序,给应用软件的维护工作带来极大的不便。因此有很多程序员现在已经充分利用EXECL的强大...

MD5加密算法VB源码

今天在在网页的源代码中找到了MD5加密算法的代码,呵呵,搬过来VB用了~~ 直接调用就可以了  Private Const BITS_TO_A_BYTE = 8 P...

VB UTC+8校正 源码

Function HttpGet(url) With CreateObject("Msxml2.ServerXMLHTTP") .open "GET", url,...

Base64加密解密VB源码

base64加密模块 用法:Base64Encode(‘加密字符') Option Explicit Publ...

VB获取网页中的验证码

VB获取网页中的验证码 函数代码: Public Function GetImg(WebBrowser, Img, sxz) '参数 'WebBrowser:等获取验...

[VB函数]将资源文件输出到指定目录下

Private Function UnRes(ByVal ResID As Integer, ByVal ResName As String, ByVal UnResPath As String)...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。