12'tableタグを一つずつを変数objにセット
3 For Each obj In ieDoc.getElementsByTagName("table")
45 'trタグを一つずつを変数objにセット
6 For Each obj2 In obj.getElementsByTagName("tr")
7 j = j + 1
8 For Each obj3 In obj2.getElementsByTagName("td")
910 Debug.Print obj3.innerHTML
11 ↑ここの情報にaタグになっている要素があります。
12 ここにaタグが含まれていた場合は、ExcelシートにinnerTextを記載する際に特別な処理を入れたいと考えております。
1314 i = i + 1
15 'タグのテキスト内容をセルにセット
16 Worksheets("Sheet1").Cells(j, i).Value = obj3.innerText
1718 Next
19 Next
20 Next
21コード
Option Explicit
Sub Test_Sample_Miniature()
Dim IE As InternetExplorerMedium
Dim ieDoc As HTMLDocument
Dim objs As IHTMLElementCollection
Dim obj As IHTMLElement
Dim j As Integer '行
Dim i As Integer '列
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate ThisWorkbook.Path & "\" & "Test_Sample_Miniature.html"
Do While IE.Busy Or IE.ReadyState < READYSTATE_COMPLETE
DoEvents
Loop
Set ieDoc = IE.Document
'----------------------------------------
'table値を取得し正規表現にてaタグ値を取得
'----------------------------------------
Dim MyStr As String
Dim reg As New RegExp
Dim mclTR As MatchCollection
Dim matTR As Match
Dim mclTD As MatchCollection
Dim matTD As Match
Dim mclA As MatchCollection
Dim matA As Match
j = 0: i = 0
'階層(1)table
For Each obj In ieDoc.getElementsByTagName("table")
'
j = j + 1: i = 1
Worksheets("Sheet1").Cells(j, i).Value = "( " & j & " - " & i & " )" & Replace(obj.innerText, vbCr, "")
Worksheets("Sheet1").Cells(j, i).WrapText = False
'
reg.Pattern = "(<tr|<TR).[\s\S]*?(</tr>|</TR>)"
reg.Global = True
Set mclTR = reg.Execute(obj.innerHTML)
'階層(2)tr
For Each matTR In mclTR
'
j = j + 1: i = 2
Worksheets("Sheet1").Cells(j, i).Value = "( " & j & " - " & i & " )" & Replace(matTR, vbCr, "")
Worksheets("Sheet1").Cells(j, i).WrapText = False
'
reg.Pattern = "(<td|<TD).[\s\S]*?(</td>|</TD>)"
reg.Global = True
Set mclTD = reg.Execute(matTR)
'階層(3)td
For Each matTD In mclTD
'
j = j + 1: i = 3
Worksheets("Sheet1").Cells(j, i).Value = "( " & j & " - " & i & " )" & Replace(matTD, vbCr, "")
Worksheets("Sheet1").Cells(j, i).WrapText = False
'
reg.Pattern = "(<a|<A).[\s\S]*?(</a>|</A>)"
reg.Global = True
Set mclA = reg.Execute(matTD)
'階層(4)a
For Each matA In mclA
'
j = j + 1: i = 4
MyStr = Mid(matA, InStr(matA, ">") + 1, Len(matA) - Len("</a>") - InStr(matA, ">"))
Worksheets("Sheet1").Cells(j, i).Value = "( " & j & " - " & i & " )" & MyStr
Worksheets("Sheet1").Cells(j, i).WrapText = False
'
Next
'
Next
'
Next
'
Next
End Sub
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/03 12:34