有难度问题哦,WebBrowser控件如何获取和IE“查看源文件”功能一致的网页Html源代码?

tank99 2008-04-02 09:14:17
尝试过WebBrowser控件,虽然能获得Hmtl源代码,可是却并和IE“查看源文件”功能一致的网页Html源代码,用Inet控件可以获得一致,但唯独WebBrowser搞不出来,有高人指点下吗?谢谢~~
...全文
375 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zengxin2008 2010-08-22
我也有这个问题,只要是通过Ajax动态加载过来的内容,在查看源码的时候就看不到?有谁知道吗?
回复
tank99 2008-04-04
找到一个更快捷直接的方法:
WebBrowser1.Document.Body.OuterHtml
回复
tank99 2008-04-03
谢谢vbman2003,您的代码调试通过,可行,虽然美中不足是多了比较多重复的段落,但无所谓了。

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
回复
kmzs 2008-04-03
用DOM读。
回复
vbman2003 2008-04-03
http://topic.csdn.net/u/20080401/15/2766c8c9-8c6c-4bb0-8b82-b00b22f7b779.html
回复
tank99 2008-04-03
求大家都帮帮忙啊~
回复
tank99 2008-04-03
有朋友知道不?这个问题很困扰啊,并且我必须是通过WebBrowser,而不通过Inet
回复
forbearORfolie 2008-04-02
貌似把.html更名为.txt的那种
回复
tank99 2008-04-02
谢谢楼上,这些网络上的方法我都测试过了,都是不能获取和IE那种一致的网页源代码的。
回复
我在网上找到使用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 "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

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

'引用 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 "http://www.sdqx.gov.cn/sdcity.php"
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", "http://www.sina.com", 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("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

End If

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

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文件中如果这个网页中含有框架那么要对框加进行处理。

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


http://blog.csdn.net/surro/archive/2007/08/10/1737599.aspx
回复
tank99 2008-04-02
帮帮忙啊,HELP ME,UP
回复
相关推荐
基于java的企业人事管理系统设计--软件工程课程设计(含源码与论文设计).rar 1 引言 4 1.1 课程设计目标 4 1.2 编程工具(编程环境)介绍 4 1.3 实施时间及主要实施步骤 4 2 需求分析 5 3 系统总体设计 6 4 数据库设计 6 5 主要功能模块的设计与实现 10 5.1 功能模块1详细设计(综合查询员工信息)..........................................................10 5.1.1 详细设计.......................................................................................................10 5.1.2 算法流程........................................................................................................15 5.1.3 界面设计及测试结果.....................................................................................15 6 调试分析 15 7 用户手册 16 8 测试结果 17 8.1 员工信息的添加...................................................................................................17 8.2 员工信息的修改...................................................................................................17 8.3 员工信息的删除...................................................................................................18 8.4 员工信息的综合查询...........................................................................................18 8.5 员工信息按性别统计的结果................................................................................19 8.6 员工信息按状态统计的结果...............................................................................19 8.7 员工信息按职称统计的结果................................................................................20 9 结论 20 10 参考文献 20
发帖
VB基础类
创建于2007-09-28

7492

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-04-02 09:14
社区公告
暂无公告