vbman2003的代码,基本解决WebBrowser控件获取和IE“查看源文件”功能一致的网页
'定义一个全局变量:
Dim b As Boolean
Private Sub Command1_Click()
b = False
WebBrowser1.Navigate (Text1.Text)
End Sub
Private Sub Command2_Click()
Dim s As String
Dim i As Long
While b = False
DoEvents
Wend
For i = 0 To WebBrowser1.Document.All.length - 1
s = s & WebBrowser1.Document.All(i).innerHTML
Next
RichTextBox1.Text = s
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'页面下载完毕
If WebBrowser1.Busy = False Then
b = True
End If
End Sub
Private Sub Command1_Click()
web.Navigate "www.google.com"
End Sub
Private Sub web_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Set doc = web.Document
For Each i In doc.All
msgbox typename(i)
Text1.Text = Text1.text & vbclrf & i.innertext
Next
End sub
Private Sub WebBrowser1_DownloadComplete()
Dim objHtml As Object
'下载完成时状态栏显示“Link Finished”
Set objHtml = Me.WebBrowser1.Document.Body.Createtextrange()
If Not IsNull(objHtml) Then
Text1.Text = objHtml.htmltext
End If
End Sub
使用inet控件
Source1 = Inet1.OpenURL("www.csdn.net")
If Source1 <> "" Then
RichTextBox1.Text = Source1
Me.Inet1.Cancel
Else
Source = MsgBox("Source code is not available.", vbInformation, "Source Code")
End If
Private Sub Command1_Click()
Text1.Text = WebBrowser1.Document.body.innerHTML
End Sub
==================================================================================
加入timer,commandbutton,text
private sub command1_click()
webbrowser1.navigate http://www.sohu.com/
timer1.enabled=true
end sub
private sub timer1_timer()
dim doc,objhtml as object
dim i as integer
dim strhtml as string
if not webbrowser1.busy then
set doc=webbrowser1.document
i=0
set objhtml=doc.body.createtextrange()
if not isnull(objhtml) then
text1.text=objhtml.htmltext
end if
timer1.enabled=false
end if
end sub
Dim doc, objhtml As Object
If Not webbrowser1.Busy Then
Set doc = webbrowser1.Document
Set objhtml = doc.body.createtextrange()
If Not IsNull(objhtml) Then
text1.text=objhtml.htmltext
End If
Set doc = Nothing
Set objhtml = Nothing
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _
ByVal sAgent As String, ByVal lAccessType As Long, _
ByVal sProxyName As String, ByVal sProxyBypass As String, _
ByVal lFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" ( _
ByVal hInternetSession As Long, ByVal sUrl As String, _
ByVal sHeaders As String, ByVal lHeadersLength As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet.dll" ( _
ByVal hFile As Long, ByVal sBuffer As String, _
ByVal lNumBytesToRead As Long, _
lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _
ByVal hInet As Long) As Integer
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Dim s
Private Function GetUrlFile(stUrl As String) As String
Dim lgInternet As Long, lgSession As Long
Dim stBuf As String * 1024
Dim inRes As Integer
Dim lgRet As Long
Dim stTotal As String
stTotal = vbNullString
lgSession = InternetOpen("VBTagEdit", 1, vbNullString, vbNullString, 0)
If lgSession Then
lgInternet = InternetOpenUrl(lgSession, stUrl, vbNullString, _
0, INTERNET_FLAG_NO_CACHE_WRITE, 0)
If lgInternet Then
Do
inRes = InternetReadFile(lgInternet, stBuf, 1024, lgRet)
stTotal = stTotal & Mid$(stBuf, 1, lgRet)
Loop While (lgRet <> 0)
End If
inRes = InternetCloseHandle(lgInternet)
End If
GetUrlFile = stTotal
End Function
Private Sub Command1_Click()
Text1.Text = GetUrlFile("http://adsl.tsee.net/teleplay/view.asp?id=143")
End Sub
Set vDoc = WebBrowser1.Document
'获取网页的源码
For Each o In vDoc.All
DoEvents
htmlpage = htmlpage & o.innerHTML
Next
然后用写二进制文件的方法将htmlpage的内容写入到.html文件中如果这个网页中含有框架那么要对框加进行处理。