VBA to fetch html URL from webpage

对着背影说爱祢 提交于 2020-07-11 05:28:42

问题


I have created Macro which can read all the HTML of provided URL, however I want to fetch all the url from that HTML.

    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

I tried to code this to fetch the URLs however its giving "Object Required error"

can anyone please help to modify this macro which will help me to fetch all the URL from HTML page.

I am using www.mini.in website for testing.

Mayur.


回答1:


Try this :

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


来源:https://stackoverflow.com/questions/38892656/vba-to-fetch-html-url-from-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!