vb webbrowser 保存网页源码的各种…

我在网上找到使用rft控件保存webbrowse文本 txtHtml是RichTextBox
txtHTML.Text = Webbrowser1.document.body.innerText
´flag :rsftext 保存为txt文件,strtmp文件路径
txtHTML.saveFile strtmp,rtfText


将其name属性设置为web

Private Sub Command1_Click()
web.Navigate ""
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


===========================================================================================
转载

´引用 Microsoft HTML Object Library


Dim odoc As HTMLDocument
Dim oElement As Object
Dim oTxtRgn As Object
Dim sSelectedText As String

Set odoc = Webbrowser1.Document´获得文档对象
Set oElement = odoc.getElementById("T1")´获得ID="T1"的对象
Set oTxtRgn = odoc.selection.createrange´获得文档当前正选择的区域对象

sSelectedText = oTxtRgn.Text´选择区域文本赋值

oElement.Focus´"T1"对象获得焦点

oElement.Select´全选对象"T1"

Debug.Print "你选择了文本:" & sSelectedText


上面这段儿还附送了其他功能,呵呵。精简一下是这样:
Dim odoc As Object
Dim oTxtRgn As Object
Dim sSelectedHTML As String

Set odoc = Webbrowser1.Document ´获得文档对象
Set oTxtRgn = odoc.selection.createrange ´获得文档当前正选择的区域对象

sSelectedHTML = oTxtRgn.htmlText ´选择区域文本赋值


Text1.Text=sSelectedHTML ´文本框显示抓取得HTML源码
......´或者继续分析源码


==================================================================================================

我用Webbrowser取得网页源码,直接运行正常,但在编译后出错
Private Sub Command1_Click()
Webbrowser1.Navigate ""
End Sub

Private Sub Webbrowser1_DownloadComplete()
´页面下载完毕
Dim doc,objhtml
Set doc = Webbrowser1.Document

Set objhtml = doc.body.createtextrange()
If Not IsNull(objhtml) Then
Text1.Text = objhtml.htmltext
End If


End Sub

我用Webbrowser取得网页源码,直接运行正常,但在编译后出错


提示:实时错误“91” Object 变量或 with 块变量没有设置
可能是没有下载完所致,

Private Sub Webbrowser1_DownloadComplete()
if webbrowser.busy=false then
Dim doc,objhtml
Set doc = Webbrowser1.Document


Set objhtml = doc.body.createtextrange()
If Not IsNull(objhtml) Then
Text1.Text = objhtml.htmltext
End If
end if
End Sub

你要得网页源码用 xmlhttp比较好


先引用 msxml

Dim x As New MSXML2.XMLHTTP
x.open "get","",False
x.send

MsgBox StrConv(x.responseBody,vbUnicode)

===============================================================================================
我在网上找到使用rft控件保存webbrowse文本 txtHtml是RichTextBox
txtHTML.Text = Webbrowser1.document.body.innerText
´flag :rsftext 保存为txt文件,strtmp文件路径
txtHTML.saveFile strtmp,rtfText


=====================================================================================


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("")
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
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

End If

===================================================================================================
或者试试用InternetReadFile,效果也可以:
Option Explicit

Private Declare Function Internetopen Lib "wininet.dll" Alias "InternetopenA" ( _
ByVal sAgent As String,ByVal lAclearcase/" target="_blank" >ccesstype 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,0)
If lgSession Then
lginternet = InternetopenUrl(lgSession,stUrl,_
0,INTERNET_FLAG_NO_CACHE_WRITE,0)
If lginternet Then
Do

inRes = InternetReadFile(lginternet,stBuf,1024,lgRet)
stTotal = stTotal & Mid$(stBuf,lgRet)
Loop While (lgRet <> 0)
End If
inRes = InternetCloseHandle(lginternet)
End If
GetUrlFile = stTotal
End Function


Private Sub Command1_Click()
Text1.Text = GetUrlFile("")
End Sub

=====================================================================================================

Set vDoc = Webbrowser1.Document ´获取网页的源码 For Each o In vDoc.All DoEvents htmlpage = htmlpage & o.innerHTML Next 然后用写二进制文件方法将htmlpage的内容写入到.html文件中如果这个网页中含有框架那么要对框加进行处理。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...