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

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 cutemouse 的 blog

我在网上找到使用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


本文关键:webbrowser
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top