vb-跨域访问网页最简单方法,获得特定的frame进行处理
以下三种方法所用时间为130,82,28微秒(一秒=100万微秒,=1000毫秒,1毫秒=1000微秒)
Private Sub Command1_Click() Dim Doc2 As HTMLDocument Dim Web2 As WebBrowser_V1 Set Web2 = GetFrameIframeLikeUrl(WebBrowser1.Document,"*baidu.com*") Set Web2 = GetFrameLikeUrl(WebBrowser1.Document,"*baidu.com*") Set Web2 = FindFrameByUrl(WebBrowser1.Document,"*baidu.com*") If Not Web2 Is Nothing Then Set Doc2 = Web2.Document 'MsgBox "框架网页中的文字是:" & Doc2.body.innerText End If End Sub '以下三种方法所用时间为130,28 微秒(一秒=100万微秒,=1000毫秒,1毫秒=1000微秒) Function GetFrameIframeLikeUrl(Vdoc As HTMLDocument,LikeUrl As String) As WebBrowser_V1 Dim Vtag,Tname As String,FrameWeb As WebBrowser_V1 For Each Vtag In Vdoc.All Tname = Vtag.tagName If Tname = "IFRAME" Or Tname = "FRAME" Then Set FrameWeb = Vtag If FrameWeb.LocationURL Like LikeUrl Then Set GetFrameIframeLikeUrl = FrameWeb Exit Function End If End If Next End Function Function GetFrameLikeUrl(Vdoc As HTMLDocument,LikeUrl As String) As WebBrowser_V1 Dim FrameWeb As WebBrowser_V1,MyFrames As Object,I As Long Set MyFrames = Vdoc.getElementsByTagName("FRAME") For I = 0 To MyFrames.length - 1 Set FrameWeb = MyFrames(I) If FrameWeb.LocationURL Like LikeUrl Then Set GetFrameLikeUrl = FrameWeb Exit Function End If Next End Function Function FindFrameByUrl(Doc As HTMLDocument,LikeUrl As String) As WebBrowser_V1 '方法2:按网址得到跨域的web ''DOC为要处理的webbrowser.DOCUMENT '这个方法要引用OLELIB.TLB http://www.mvps.org/emorcillo/download/vb6/tl_ole.zip On Error Resume Next Dim pContainer As olelib.IOleContainer Dim pEnumerator As olelib.IEnumUnknown Dim pUnk As olelib.IUnknown Dim pBrowser As WebBrowser_V1 Set pContainer = Doc If pContainer.EnumObjects(OLECONTF_EMBEDDINGS,pEnumerator) = 0 Then Do While pEnumerator.Next(1,pUnk) = 0 Set pBrowser = pUnk If pBrowser.LocationURL Like LikeUrl Then '可以在这里加条件判断得到指定的frame,基本可以根据url或者innerHTML中的某个关键字符 Set FindFrameByUrl = pBrowser Exit Do End If Loop Set pEnumerator = Nothing End If DoEnd: Set pContainer = Nothing End Function '130,28