【分享】vb过滤得到网页元素的通用函数

无·法 2017-01-17 09:12:46
加精

Option Explicit

Private Declare Function SafeArrayGetDim Lib "oleaut32.dll " (ByRef saArray() As Any) As Long

'名称:getElementsByAttributes
'功能:根据一个或多个条件对dom对象所有元素进行过滤得到目标元素
'参数:WebBrowser1,WebBrowser类型,要处理的webbrowser
' strAttributes,string型,内容为属性列表,多项的话用逗号隔开,
'返回:如果有匹配到的结果那么返回的就是html元素对象数组,用户需要执行判断使用
'范例:getElementsByAttributes(WebBrowser1,"id='kw'")(0).value="vb" '设置百度搜索框内容为vb
' getElementsByAttributes(WebBrowser1,"value='百度一下',type='submit'")(0).click '点击百度的搜索按钮
' getElementsByAttributes(WebBrowser1,"tagname='input',value^='百度'")(0).click '得到文本开头为“百度”的按钮并执行点击
' Dim v
' For Each v In getElementsByAttributes(WebBrowser1, "tagname=img,src<>''")
' Print v.src
' Next

'作者:sysdzw
'日期:23:53 2017-1-17
Public Function getElementsByAttributes(WebBrowser1 As Object, ByVal strAttributes As String) As Variant
Dim vTag As Object
Dim i&, strTiaojians$, isElementOk As Boolean, intElement%, vrt()
Dim reg As Object
Dim matchs As Object
Dim strAttrName As String, strAttrValue As String

Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.IgnoreCase = True
reg.MultiLine = True
reg.Pattern = "([a-z\dA-Z-_.]+)([!=<>^$*|~]+)(['""]?)([^,]*)\3"
Set matchs = reg.Execute(strAttributes)

For Each vTag In WebBrowser1.Document.All
isElementOk = True
For i = 0 To matchs.Count - 1 '循环判断strAttributes中的多个条件,只有都符合才能返回true,如果有一个不符合就返回false并且退出其他条件的判断
strAttrName = LCase(matchs(i).SubMatches(0))
If strAttrName = "classname" Then strAttrName = "class"
If strAttrName = "tagname" Then
If Not isConditionOk(LCase(vTag.tagname), matchs(i).SubMatches(1), LCase(matchs(i).SubMatches(3))) Then
isElementOk = False
Exit For
End If
ElseIf strAttrName = "innerhtml" Then
If Not isConditionOk(LCase(vTag.innerhtml), matchs(i).SubMatches(1), LCase(matchs(i).SubMatches(3))) Then
isElementOk = False
Exit For
End If
ElseIf strAttrName = "innertext" Then
If Not isConditionOk(LCase(vTag.innertext), matchs(i).SubMatches(1), LCase(matchs(i).SubMatches(3))) Then
isElementOk = False
Exit For
End If
ElseIf strAttrName = "class" Then
strAttrValue = "NULL"
If Not IsNull(vTag.getattribute("class")) Then
strAttrValue = vTag.getattribute("class")
ElseIf Not IsNull(vTag.getattribute("classname")) Then
strAttrValue = vTag.getattribute("classname")
End If
If strAttrValue = "NULL" Then
isElementOk = False
Exit For
ElseIf Not (isConditionOk(strAttrValue, matchs(i).SubMatches(1), LCase(matchs(i).SubMatches(3))) Or isConditionOk(strAttrValue, matchs(i).SubMatches(1), LCase(matchs(i).SubMatches(3)))) Then
isElementOk = False
Exit For
End If
ElseIf IsNull(vTag.getattribute(strAttrName)) Then
isElementOk = False
Exit For
ElseIf Not isConditionOk(vTag.getattribute(strAttrName), matchs(i).SubMatches(1), matchs(i).SubMatches(3)) Then
isElementOk = False
Exit For
End If
Next

If isElementOk Then
ReDim Preserve vrt(intElement)
Set vrt(intElement) = vTag
intElement = intElement + 1
End If
Next
If SafeArrayGetDim(vrt) = 0 Then
getElementsByAttributes = Split("", "")
Else
getElementsByAttributes = vrt
End If
End Function
'简略的调用方式
Public Function g(WebBrowser1 As Object, ByVal strAttributes As String) As Variant
g = getElementsByAttributes(WebBrowser1, strAttributes)
End Function
'根据运算符检查条件是否符合
Private Function isConditionOk(ByVal strTagValue$, ByVal strCondition$, ByVal strValueForCheck$) As Boolean
If strCondition = "=" Then
isConditionOk = (strTagValue = strValueForCheck)
ElseIf strCondition = "!=" Or strCondition = "<>" Then
isConditionOk = (strTagValue <> strValueForCheck)
ElseIf strCondition = "^=" Then '选取开头为strValueForCheck的
isConditionOk = (Left(strTagValue, Len(strValueForCheck)) = strValueForCheck)
ElseIf strCondition = "$=" Then '选取末尾为strValueForCheck的
isConditionOk = (Right(strTagValue, Len(strValueForCheck)) = strValueForCheck)
ElseIf strCondition = "*=" Then '选取包含strValueForCheck的
isConditionOk = (InStr(strTagValue, strValueForCheck) > 0)
ElseIf strCondition = "|=" Then '选取值为strValueForCheck或者值为strValueForCheck前缀的,即strValueForCheck后面加个-
isConditionOk = (InStr(strTagValue, strValueForCheck) > 0 Or InStr(strTagValue, strValueForCheck & "-") > 0)
ElseIf strCondition = "~=" Then '选取属性值用空格分隔的值中包含给定值的元素
isConditionOk = (InStr(" " & strTagValue & " ", " " & strValueForCheck & " ") > 0)
End If
End Function




使用范例,获取所有src属性不为空的元素并打印出来
Private Sub Command1_Click()
Dim v
For Each v In getElementsByAttributes(WebBrowser1, "tagname=img,src<>''")
Print v.src
Next
End Sub


条件可以组合,比如得到input按钮上名字为搜索的按钮对象,并且name不为search那么strAttributes条件就用"tagname='input',value='搜索',name!='search'"
...全文
12035 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
无·法 2019-05-13
  • 打赏
  • 举报
回复
引用 30 楼 weixin_40174192 的回复:
可以再举个详细一点的使用方法吗,有点不太懂,例如要拿到a标签,并且class="bcd"的内容怎么办


Private Sub Command1_Click()
Dim v
For Each v In getElementsByAttributes(WebBrowser1, "tagname=a,class=bcd")
Print v.href
Next
End Sub
猿使人有钱 2019-05-13
  • 打赏
  • 举报
回复
可以再举个详细一点的使用方法吗,有点不太懂,例如要拿到a标签,并且class="bcd"的内容怎么办
VB业余爱好者 2018-12-05
  • 打赏
  • 举报
回复
强大的正则表达式
6742 2017-02-13
  • 打赏
  • 举报
回复
linsondean 2017-02-13
  • 打赏
  • 举报
回复
正则表达式是个好东西
无·法 2017-02-11
  • 打赏
  • 举报
回复
引用 25 楼 Topc008 的回复:
呵呵,支持楼主分享。 不知道利用document.all和callback是不是可以实现?没试验过,如果可行的话,应该更简单
这个dom这么会和callback扯上联系?不明白
一如既往哈 2017-02-10
  • 打赏
  • 举报
回复
呵呵,支持楼主分享。 不知道利用document.all和callback是不是可以实现?没试验过,如果可行的话,应该更简单
狮子座鹰 2017-02-09
  • 打赏
  • 举报
回复
收藏,值得拥有
xiaoxiangqing 2017-02-07
  • 打赏
  • 举报
回复
cattpon 2017-02-04
  • 打赏
  • 举报
回复
learning~
hugh_z 2017-02-03
  • 打赏
  • 举报
回复
6666666666666
cattpon 2017-02-03
  • 打赏
  • 举报
回复
learning~
三断笛 2017-02-02
  • 打赏
  • 举报
回复
把jquery加载到DOM就更简单更强大啦
只有150M 2017-02-02
  • 打赏
  • 举报
回复
好高深。没有看懂。支持了
hugh_z 2017-02-01
  • 打赏
  • 举报
回复
6666666666
nettman 2017-01-31
  • 打赏
  • 举报
回复
cattpon 2017-01-31
  • 打赏
  • 举报
回复
learning~
hugh_z 2017-01-31
  • 打赏
  • 举报
回复
666666666666666
cattpon 2017-01-30
  • 打赏
  • 举报
回复
learning~
hugh_z 2017-01-30
  • 打赏
  • 举报
回复
6666666
加载更多回复(9)
CruiseYoung提供的带有详细书签的电子书籍目录 http://blog.csdn.net/fksec/article/details/7888251 该资料是《Visual C++ 2005入门经典》的源代码及课后练习答案 对应的书籍资料见: Visual C++ 2005入门经典 基本信息 原书名: Ivor Horton's Beginning Visual C++ 2005 原出版社: Wiley 作者: (美)Ivor Horton 译者: 李颂华 康会光 出版社:清华大学出版社 ISBN:9787302142713 上架时间:2007-2-12 出版日期:2007 年1月 开本:16开 页码:1046 版次:1-1 编辑推荐   本书由编程语言先驱者Ivor Horton倾力而著,是国内第一本全面、深入介绍Visual C++ 2005的经典之作! 内容简介   本书系编程语言先驱者Ivor Horton的经典之作,是学习C++编程最畅销的图书品种之一,不仅涵盖了Visual C++ .NET编程知识,还全面介绍了标准C++语言和.NET C++/CLI。本书延续了Ivor Horton讲解编程语言的独特方法,从中读者可以学习Visual C++ 2005的基础知识,并全面掌握在MFC和Windows Forms中访问数据源的技术。此外,本书各章后面的习题将有助于读者温故而知新,并尽快成为C++高效程序员。...    作译者   Ivor Horton是世界著名的计算机图书作家,主要从事与编程相关的顾问及撰写工作,曾帮助无数程序员步入编程的殿堂。他曾在IBM工作多年,能使用多种语言进行编程(在多种机器上使用汇编语言和高级语言),设计和实现了实时闭环工业控制系统。Horton拥有丰富的教学经验(教学内容包括C、C++、Fortran、PL/1、APL等),同时还是机械、加工和电子CAD系统、机械CAM系统和DNC/CNC系统方面的专家。Ivor Horton还著有Beginning Visual C++ 6、Beginning C Programming和Beginning Java 2等多部入门级好书。 目录 封面 -18 前言 -14 目录 -9 第1章 使用Visual C++ 2005编程 1 1.1 .NET Framework 1 1.2 CLR 2 1.3 编写C++应用程序 3 1.4 学习Windows编程 4 1.4.1 学习C++ 4 1.4.2 C++标准 5 1.4.3 控制台应用程序 5 1.4.4 Windows编程概念 6 1.5 集成开发环境简介 7 1.6 使用IDE 9 1.6.1 工具栏选项 9 1.6.2 可停靠的工具栏 10 1.6.3 文档 11 1.6.4 项目和解决方案 11 1.6.5 设置Visual C++ 2005的选项 23 1.6.6 创建和执行Windows应用程序 24 1.6.7 创建Windows Forms应用程序 26 1.7 小结 29 第2章 数据、变量和计算 31 2.1 C++程序结构 31 2.1.1 程序注释 36 2.1.2 #include指令——头文件 37 2.1.3 命名空间和using声明 37 2.1.4 main()函数 38 2.1.5 程序语句 38 2.1.6 空白 40 2.1.7 语句块 41 2.1.8 自动生成的控制台程序 41 2.2 定义变量 42 2.2.1 命名变量 43 2.2.2 C++中的关键字 43 2.2.3 声明变量 44 2.2.4 变量的初值 44 2.3 基本数据类型 45 2.3.1 整型变量 45 2.3.2 字符数据类型 46 2.3.3 整型修饰符 47 2.3.4 布尔类型 48 2.3.5 浮点类型 48 2.3.6 ISO/ANSI C++中的基本类型 49 2.3.7 字面值 50 2.3.8 定义数据类型的同义词 50 2.3.9 具有特定值集的变量 51 2.3.10 指定枚举常量的类型 52 2.4 基本的输入/输出操作 53 2.4.1 从键盘输入 53 2.4.2 到命令行的输出 53 2.4.3 格式化输出 54 2.4.4 转义序列 55 2.5 C++中的计算 57 2.5.1 赋值语句 57 2.5.2 算术运算 58 2.5.3 计算余数 63 2.5.4 修改变量 63 2.5.5 增量和减量运算符 64 2.5.6 计算

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧