VBA从类表返回html元素href
我有这个html结构:VBA从类表返回html元素href,html,excel,vba,web-scraping,element,Html,Excel,Vba,Web Scraping,Element,我有这个html结构: <table class="series"> <tr> <th>1.</th> <td><div><a href="?id=12">1st part</a></div></td> </tr> <tr> <th>2.</th> <td><div><
<table class="series">
<tr>
<th>1.</th>
<td><div><a href="?id=12">1st part</a></div></td>
</tr>
<tr>
<th>2.</th>
<td><div><a href="?id=13">2nd part</a></div></td>
</tr>
<tr>
<th>3.</th>
<td><div><a href="?id=14">3rd part</a></div></td>
</tr>
<tr>
<th>4.</th>
<td><div><a href="?id=15">4th part</a></div></td>
</tr>
<tr>
<th>5.</th>
<td><b>5th part</b></td>
</tr>
<tr>
<th>6.</th>
<td><div><a href="?id=16">6th part</a></div></td>
</tr>
- 这只是为了调试元素中的每个href,但是for循环不起作用,它总是只调试一次
而且,如果我计算了调试,我现在仍然不知道如何得到上一部分的url结果:(如果有多个系列表,并且您需要所有链接,那么您需要遍历所有系列(您已经这样做了),然后像这样遍历每个系列中的所有链接
Set my_data=.Document.getElementsByClassName(“系列”)
调暗所有链接,链接
长度为的尺寸i:i=2
对于my_数据中的每个元素
设置所有链接=elem.getElementsByTagName(“a”)
对于所有链接中的每个链接
'将数据复制到excel工作表
Debug.Print link.href
Debug.Print link.innerText
i=i+1
下一个
下一个
使用以下假设
no class 'series' then null (or better to use table.series ?) With just series assumption is it only occurs for table element
if no bold in table then null
else iterate rows using counter
if bold found:
if row = 0 then
null
else test row - 1 (prior row)
if has single href attribute then href else null
您可以编写一个select case语句和一个代理htmldocument变量来应用辅助函数中的每个测试
Option Explicit
Public Sub PrintPriorHref()
Dim html As MSHTML.HTMLDocument, table As MSHTML.HTMLTable, ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
Set html = New MSHTML.HTMLDocument
With ie
.Visible = True
.navigate "www.something.com/do.php?id=5"
Do: DoEvents: Loop While .Busy Or .readyState <> READYSTATE_COMPLETE
html.body.innerHTML = .document.body.innerHTML
Set table = html.querySelector(".series")
Debug.Print GetPriorHref(table)
.Quit
End With
End Sub
Public Function GetPriorHref(ByVal table As MSHTML.HTMLTable) As Variant
Dim i As Long, html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
Select Case True
Case table Is Nothing
GetPriorHref = Null ' "Null"
Case table.getElementsByTagName("b").Length <> 1
GetPriorHref = Null
Case Else
Dim r As MSHTML.HTMLTableRow
For Each r In table.rows
html.body.innerHTML = r.outerHTML
If html.querySelectorAll("b").Length > 0 Then
Select Case i
Case 0
GetPriorHref = Null '"Null"
Case Is > 0
Dim anchorList As Object
html.body.innerHTML = table.rows(i - 1).outerHTML
Set anchorList = html.querySelectorAll("[href]")
If anchorList.Length <> 1 Then
GetPriorHref = Null ' "Null"
Else
GetPriorHref = anchorList(0).href
End If
End Select
End If
i = i + 1
Next
End Select
End Function
选项显式
公共子打印机priorhref()
将html设置为MSHTML.HTMLDocument,将表格设置为MSHTML.HTMLTable,即设置为SHDocVw.InternetExplorer
设置ie=New SHDocVw.InternetExplorer
Set html=New MSHTML.HTMLDocument
与ie
.Visible=True
.浏览“www.something.com/do.php?id=5”
Do:DoEvents:Loop While.Busy或.readyState readyState\u COMPLETE
html.body.innerHTML=.document.body.innerHTML
Set table=html.querySelector(“.series”)
Debug.Print GetPriorHref(表)
退出
以
端接头
公共函数GetPriorHref(ByVal表为MSHTML.HTMLTable)作为变量
Dim i与MSHTML.HTMLDocument一样长,html与MSHTML.HTMLDocument一样长
Set html=New MSHTML.HTMLDocument
选择Case True
病例表什么都不是
GetPriorHref=Null“Null”
案例表.getElementsByTagName(“b”).长度1
GetPriorHref=Null
其他情况
将r设置为MSHTML.HTMLTableRow
对于table.rows中的每个r
html.body.innerHTML=r.outerHTML
如果html.querySelectorAll(“b”).Length>0,则
选择案例一
案例0
GetPriorHref=Null“Null”
大小写>0
作为对象的模糊主播
html.body.innerHTML=table.rows(i-1).outerHTML
Set anchorList=html.querySelectorAll(“[href]”)
如果主持人。长度为1,则
GetPriorHref=Null“Null”
其他的
GetPriorHref=anchorList(0)。href
如果结束
结束选择
如果结束
i=i+1
下一个
结束选择
端函数
所需参考资料(VBE>工具>参考资料):
代码正在迭代所有的
系列
类,因为这些类似乎只属于一个表,这是有意义的。您没有遍历表中的所有元素,只遍历具有系列
的所有元素,因为它们是类。请尝试替换Set my_data=.Document.getElementsByClassName(“系列”)
withSet my_data=.Document.getElementsByClassName(“系列”).getElementsByTagName(“a”)
并遍历该集合。当我用我的_数据替换该行,然后得到:运行时错误“438”:对象不支持此属性或方法我的错误,我没有在类名
集合中选择元素,请尝试此操作。设置我的_数据=.Document.GetElementsByCassName(“series”)(0)。getElementsByTagName(“a”)
。这将使用系列的类来迭代第一个标记中的所有a
标记。这非常有效!但老实说,目前我还没有adia如何实现我在初始问题中的结果,如果类不存在,则返回NULL,第1部分的NULL没有href,或者返回当前tr之上的href(是没有href的那一个)。我也能请你帮忙吗?
Option Explicit
Public Sub PrintPriorHref()
Dim html As MSHTML.HTMLDocument, table As MSHTML.HTMLTable, ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
Set html = New MSHTML.HTMLDocument
With ie
.Visible = True
.navigate "www.something.com/do.php?id=5"
Do: DoEvents: Loop While .Busy Or .readyState <> READYSTATE_COMPLETE
html.body.innerHTML = .document.body.innerHTML
Set table = html.querySelector(".series")
Debug.Print GetPriorHref(table)
.Quit
End With
End Sub
Public Function GetPriorHref(ByVal table As MSHTML.HTMLTable) As Variant
Dim i As Long, html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
Select Case True
Case table Is Nothing
GetPriorHref = Null ' "Null"
Case table.getElementsByTagName("b").Length <> 1
GetPriorHref = Null
Case Else
Dim r As MSHTML.HTMLTableRow
For Each r In table.rows
html.body.innerHTML = r.outerHTML
If html.querySelectorAll("b").Length > 0 Then
Select Case i
Case 0
GetPriorHref = Null '"Null"
Case Is > 0
Dim anchorList As Object
html.body.innerHTML = table.rows(i - 1).outerHTML
Set anchorList = html.querySelectorAll("[href]")
If anchorList.Length <> 1 Then
GetPriorHref = Null ' "Null"
Else
GetPriorHref = anchorList(0).href
End If
End Select
End If
i = i + 1
Next
End Select
End Function