VBA从网页获取html URL

时间:2016-08-11 09:39:55

标签: html excel vba excel-vba macros

我创建了Macro,它可以读取所提供的URL的所有HTML,但我想从该HTML中获取所有url。

    Sub GetHTML_Click()

Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim j As Integer
Set ie = New InternetExplorer
ie.Visible = True

url = Cells(1, 2)

ie.navigate url

Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.document
'Dim htmltext As Collection
Dim htmltext As String
htmltext = html.DocumentElement.innerHTML
'Need to add iURL
Dim htmlurl As IHTMLElement
For Each htmlurl In hmtltext
iurl = htmlurl.toString
Cells(j, 1).Value = CLng(iurl)
j = j + 1
Next

End Sub

我尝试对此进行编码以获取网址,但是它提供了#34;对象必需错误&#34;

任何人都可以帮助修改此宏,这将帮助我从HTML页面获取所有URL。

我正在使用www.mini.in网站进行测试。

MAYUR。

1 个答案:

答案 0 :(得分:0)

试试这个:

Dim ie As Object
Dim html As Object
Dim j As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "google.com"
ie.Navigate URL
Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.Document
'Dim htmltext As Collection
Dim htmlElements As Object
Dim htmlElement As Object
Set htmlElements = html.getElementsByTagName("*")
For Each htmlElement In htmlElements
    If htmlElement.getAttribute("href") <> "" Then Debug.Print htmlElement.getAttribute("href")
Next