Reputation: 3460
I want to know if a certain keyword exists in entire website.
How can I do it?
Quick googling suggested this way
"Googling 101)
.. just type your search terms, followed by site:www.website.com
But I am not sure how to test if it returns positive or negative.
Can anyone help?
Upvotes: 0
Views: 1571
Reputation: 8531
Something like this also
Function FIND_IN_PAGE(strURL As String, strSearch As String)
Dim pos As Long
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Set ie = New SHDocVw.InternetExplorer
ie.Visible = 1
ie.navigate strURL
Do Until ie.readyState = READYSTATE_COMPLETE And ie.Busy = False
DoEvents
Loop
Set doc = ie.document.DocumentElement
pos = InStr(1, doc.innerText, strSearch)
FIND_IN_PAGE = pos
ie.Quit
Set ie = Nothing
Set doc = Nothing
End Function
Calling like so
FIND_IN_PAGE("http://stackoverflow.com/questions/40848321/how-to-search-for-a-keyword-in-entire-website","entire")
Upvotes: 2
Reputation: 4514
Try this, it basically checks to see if there are any google search results by searching for a keyword or phrase on the site:
Sub Check_Website()
Dim ie As Object
Dim str As String, web As String, URL As String
Dim iResults As Integer
'Create IE object
Set ie = CreateObject("InternetExplorer.Application")
'Set string to search for
str = "hello"
str = Replace(str, " ", "+")
'Set website to search in
web = "www.google.com"
'Create full URL
URL = "https://www.google.co.uk/search?q=" & str & "+site%3A" & web
'Navigate to URL
With ie
.Visible = False
.Navigate URL
Do While .ReadyState <> 4: DoEvents: Loop
End With
'Count results on first page
iResults = ie.Document.getelementsbyclassname("g").Length
'Message box dependent on results
If iResults = 0 Then
MsgBox "No matches were found."
Else
MsgBox "Matches found."
End If
ie.Quit
Set ie = Nothing
End Sub
Google uses the class name of "g" for there search results meaning there will be a maximum of 10 items in the "g" class for the particular search results page, if no results are shown there is no "g" class which means there are no items to be counted.
Upvotes: 1