从网页获取链接/ URL - Excel VBA [英] Getting Links/URL from a webpage-Excel VBA
本文介绍了从网页获取链接/ URL - Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个宏,该宏将使网页中的搜索结果链接。我这样写了
I want to write a macro which will take the search result links in a webpage. I have written like this
Sub webpage()
Dim internet As InternetExplorer
Dim internetdata As HTMLDocument
Dim internetlink As Object
Dim internetinnerlink As Object
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = True
internet.Navigate ("URL")
Do While internet.Busy
DoEvents
Loop
Do Until internet.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set internetdata = internet.Document
Set internetlink = internetdata.getElementsByTagName("a")
i = 1
For Each internetinnerlink In internetlink
ActiveSheet.Cells(i, 2) = internetinnerlink.href
i = i + 1
Next internetinnerlink
End Sub
以上代码从网页上获取所有链接,但我只需要搜索结果链接。我已经上传了一个图像,如果这是我的网页,我只需要只搜索结果链接,而不是所有的链接。请帮我解决这个问题
Above code takes all the links from the web page, but i need only the search result links. i have uploaded one image, if that is my webpage, i need to take only the search result links and not all the links. please help me to fix this
推荐答案
尝试这段代码
Sub webpage()
Dim internet As Object
Dim internetdata As Object
Dim div_result As Object
Dim header_links As Object
Dim link As Object
Dim URL As String
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = True
URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
internet.Navigate URL
Do Until internet.ReadyState >= 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set internetdata = internet.Document
Set div_result = internetdata.getelementbyid("res")
Set header_links = div_result.getelementsbytagname("h3")
For Each h In header_links
Set link = h.ChildNodes.Item(0)
Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
Next
MsgBox "done"
End Sub
这篇关于从网页获取链接/ URL - Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文