0

我试图从网页中拉出一个表格,到目前为止我已经成功地从网页中拉出一个表格,不幸的是我在表格的每一行都有一些链接,当我从网页中拉出表格时,我得到的输出没有链接,只是文本,有什么方法可以使用 VBA 从网页中提取表格,包括超链接。

这是我的代码:

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 回答