0

Webページからテーブルを取得しようとしていましたが、Webページからテーブルを取得することに成功しましたが、残念ながら、テーブルの各行にリンクがいくつかあります.Webページからテーブルを取得すると、リンクなしでテキストのみが出力されます.ハイパーリンクを含む VBA を使用して Web ページからテーブルを取得する方法はありますか。

これが私のコードです:

Sub TableExample()
Dim IE As Object
Dim doc As Object
Dim strURL As String

strURL = "HERE I USED MY URL"
' replace with URL of your choice

Set IE = CreateObject("InternetExplorer.Application")
With IE
'.Visible = True

.Navigate strURL
Do Until .readyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Set doc = IE.Document
GetAllTables doc

.Quit
End With
End Sub

Sub GetAllTables(doc As Object)

' get all the tables from a webpage document, doc, and put them in a new worksheet

Dim ws As Worksheet
Dim rng As Range
Dim tbl As Object
Dim rw As Object
Dim cl As Object
Dim tabno As Long
Dim nextrow As Long
Dim I As Long

Set ws = Worksheets.Add

For Each tbl In doc.getElementsByTagName("TABLE")
tabno = tabno + 1
nextrow = nextrow + 1
Set rng = ws.Range("B" & nextrow)
rng.Offset(, -1) = "Table " & tabno
For Each rw In tbl.Rows
For Each cl In rw.Cells
rng.Value = cl.outerText
Set rng = rng.Offset(, 1)
I = I + 1
Next cl
nextrow = nextrow + 1
Set rng = rng.Offset(1, -I)
I = 0
Next rw
Next tbl

ws.Cells.ClearFormats

End Sub
4

1 に答える 1

1

「rng.Value = cl.outerText」を実行すると、テキストのみが取得されます。すべてのリンクとその他の HTML が必要な場合は、innerHTML プロパティを使用してください。

「rng.Value = cl.outerText」を「rng.Value = cl.innerHTML」に置き換えるだけです。これにより、リンクを含む html 全体が返されます ;)

于 2014-01-29T12:56:08.323 に答える