VBA从网页获取html URL [英] VBA to fetch html URL from webpage

查看:73
本文介绍了VBA从网页获取html URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了可以读取所提供URL的所有HTML的宏,但是我想从该HTML中获取所有URL.

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

我试图对此进行编码以获取URL,但是它给出了对象必需的错误"

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

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

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

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

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

Mayur.

推荐答案

尝试一下:

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

这篇关于VBA从网页获取html URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆