分享
 
 
 

获取webbrowser控件 网页的源码(收藏)

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有