如何提取第一个Google搜索结果网址? [英] How to extract first Google search result URL?
本文介绍了如何提取第一个Google搜索结果网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在A列中列出了大量搜索查询.我可以使用任何代码来提取B列中的第一个Google搜索结果网址吗?
I have a large list of search queries in column A. Is there any code I can use in order to extract the first Google search result URL in column B?
我已经成功使用了下面的代码,但没有提取第一个搜索结果URL,而是获取了搜索结果的数量.任何人都可以根据我的要求帮助我更改代码吗?
I have used the code below successfully but instead of extracting the first search result URL it gets the number of search results. Anyone can help me change the code as per my requirements?
Sub Gethits()
Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As
Object, link As Object
Dim start_time As Date
Dim end_time As Date
Dim var As String
Dim var1 As Object
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cookie As String
Dim result_cookie As String
start_time = Time
Debug.Print "start_time:" & start_time
For i = 2 To lastRow
url = "https://www.google.com/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set objResultDiv = html.getelementbyid("rso")
Set var1 = html.getelementbyid("resultStats")
Cells(i, 2).Value = var1.innerText
DoEvents
Next
end_time = Time
Debug.Print "end_time:" & end_time
Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub
推荐答案
就我而言,我使用了以下代码
In my case, I have used the following code
Sub Demo0()
Application.ScreenUpdating = False
With CreateObject("InternetExplorer.Application")
.Visible = True
For R = 5 To Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
.Navigate "https://www.google.co.in/search?q=" & Sheet1.Cells(R, 2).Text
While .Busy Or .ReadyState < 4: DoEvents: Wend
With .Document.querySelectorAll("#search div.r [href*=http]")
c = 3
For U = 0 To Application.Min(8, .Length - 1) Step 2
Sheet1.Cells(R, c) = .Item(U).href
c = c + 1
Next
End With
Next
.Quit
End With
Application.ScreenUpdating = True
End Sub
这篇关于如何提取第一个Google搜索结果网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文