gpt4 book ai didi

excel - 从网页获取链接/URL - Excel VBA

转载 作者:行者123 更新时间:2023-12-02 22:34:03 26 4
gpt4 key购买 nike

enter image description here

我想编写一个宏,它将获取网页中的搜索结果链接。我是这样写的

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

上面的代码获取了网页中的所有链接,但我只需要搜索结果链接。我上传了一张图片,如果那是我的网页,我只需要获取搜索结果链接,而不是所有链接。请帮我解决这个问题

最佳答案

试试这个代码

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

关于excel - 从网页获取链接/URL - Excel VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com