gpt4 book ai didi

html - Excel VBA打开google第一个搜索结果页

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:22 24 4
gpt4 key购买 nike

我必须使用 excel 宏打开谷歌搜索页面。在 excel 中提供搜索参数后,我能够成功打开谷歌搜索页面。但是,我的任务是打开第一个返回的搜索结果页面,并在该页面中进行一些数据提取。我使用了下面的代码。

假设我搜索“Sachin Tendulkar wiki”,我应该能够打开搜索结果中的第一页。到目前为止,我的代码如下。

Dim ie As InternetExplorer
Dim RegEx As RegExp, RegMatch As MatchCollection
Dim MyStr As String
Dim pDisp As Object
Set ie = New InternetExplorer
Set RegEx = New RegExp
Dim iedoc As Object

'Search google for "something"
ie.Navigate "http://www.google.com.au/search?hl=en&q=sachin+tendulkar+wiki&meta="

'Loop unitl ie page is fully loaded
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop



MyStr = ie.Document.body.innertext
Set RegMatch = RegEx.Execute(MyStr)

'If a match to our RegExp searchstring is found then launch this page
If RegMatch.Count > 0 Then
ie.Navigate RegMatch(0)
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
MsgBox "Loaded"
'show internet explorer
ie.Visible = True
'Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Set iedoc = ie.Application.Document
'iedoc.getElementById("divid").Value = "poS0"
'MsgBox iedoc

'ie.Navigate iedoc.getelementsbytagname("ol")(0).Children(0).getelementsbytagname("a")(0).href
ie.Navigate iedoc.getelementsbyclassname("divid")("poS0").href
Else
MsgBox "No linkedin profile found"
End If

Set RegEx = Nothing
Set ie = Nothing

我在谷歌搜索页面查看了页面源代码。我有一个特定的 div id = "pos0"这是第一个搜索结果的 id。我必须让 IE 导航到 div id = "pos0"的页面。我无法在 VBA 中完成这件事。有人可以帮帮我吗?

感谢和问候,拉梅什

最佳答案

您有几个问题。首先访问文档对象是 ie.Document 而不是 ie.Application.Document。我已经更新了您的代码以展示如何使用子字符串快速找到第一个 url。

Dim ie As InternetExplorer
Dim RegEx As RegExp, RegMatch As MatchCollection
Dim MyStr As String
Dim pDisp As Object
Set ie = New InternetExplorer
Set RegEx = New RegExp
Dim iedoc As Object

'Search google for "something"
ie.Navigate "http://www.google.com.au/search?hl=en&q=sachin+tendulkar+wiki&meta="

'Loop unitl ie page is fully loaded
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop



MyStr = ie.Document.body.innertext
Set RegMatch = RegEx.Execute(MyStr)

'If a match to our RegExp searchstring is found then launch this page
If RegMatch.Count > 0 Then
ie.Navigate RegMatch(0)
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
MsgBox "Loaded"
'show internet explorer
ie.Visible = True
'Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'****************************************
'EDITS
'****************************************
Set iedoc = ie.Document

'create a variable to hold the text
Dim extractedHTML As String
'start and end points for the substring
Dim iStart, iEnd As Integer
'get the element with ID of search - this is where the results start
extractedHTML = iedoc.getElementById("search").innerHTML
'find the first href as this will be the first link, add 1 to encompass the quote
iStart = InStr(1, extractedHTML, "href=", vbTextCompare) + Len("href=") + 1
'locate the next quote as this will be the end of the href
iEnd = InStr(iStart, extractedHTML, Chr(34), vbTextCompare)
'extract the text
extractedHTML = Mid(extractedHTML, iStart, iEnd - iStart)
'go to the URL
ie.Navigate extractedHTML

'****************************************
'End EDITS
'****************************************
Else
MsgBox "No linkedin profile found"
End If

Set RegEx = Nothing
Set ie = Nothing

关于html - Excel VBA打开google第一个搜索结果页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739552/

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