如何在 Excel 中使用 VBA 进行 Google 搜索?
How to use VBA in Excel for Google Search?
我正在尝试使用 Excel 中的 VBA 实现 Google 搜索方法。
我想在弹出窗口 window 中输入某个 word/words,然后 Google 搜索 word/words + 预定短语。
例如,如果我输入“John Doe”,我希望 Google 搜索为“John Doe License and Registration”,而不必每次输入“License and Registration”名字.
如何在我的代码中添加“许可和注册”?
Private Sub CommandButtonSearch_Click()
Dim query As String
Dim search_string As String
Dim googleChromePath As String
query = InputBox("Enter your keyword", "Google Search")
search_string = Replace(query, " ", "+")
googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)
End Sub
外观:
我希望它看起来如何:
Private Const LicenseRegistration As String = "+License+and+Registration"
Private Sub CommandButtonSearch_Click()
Dim query As String
Dim search_string As String
Dim googleChromePath As String
query = InputBox("Enter your keyword", "Google Search")
search_string = Replace(query, " ", "+") & LicenseRegistration
googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)
End Sub
也许是这样的。
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)
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("result-stats")
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
结果:
我正在尝试使用 Excel 中的 VBA 实现 Google 搜索方法。
我想在弹出窗口 window 中输入某个 word/words,然后 Google 搜索 word/words + 预定短语。
例如,如果我输入“John Doe”,我希望 Google 搜索为“John Doe License and Registration”,而不必每次输入“License and Registration”名字.
如何在我的代码中添加“许可和注册”?
Private Sub CommandButtonSearch_Click()
Dim query As String
Dim search_string As String
Dim googleChromePath As String
query = InputBox("Enter your keyword", "Google Search")
search_string = Replace(query, " ", "+")
googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)
End Sub
外观:
我希望它看起来如何:
Private Const LicenseRegistration As String = "+License+and+Registration"
Private Sub CommandButtonSearch_Click()
Dim query As String
Dim search_string As String
Dim googleChromePath As String
query = InputBox("Enter your keyword", "Google Search")
search_string = Replace(query, " ", "+") & LicenseRegistration
googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)
End Sub
也许是这样的。
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)
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("result-stats")
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
结果: