如何在整个网站中搜索关键字

时间:2016-11-28 15:48:47

标签: excel vba excel-vba web-scraping

我想知道整个网站中是否存在某个关键字。

我该怎么做?

快速谷歌搜索以这种方式建议

  

“谷歌搜索101”

     

..只需输入您的搜索字词,然后输入网站:www.website.com

但我不确定如何测试它是回报正面还是负面。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

这样的东西

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

这样打电话

FIND_IN_PAGE("http://stackoverflow.com/questions/40848321/how-to-search-for-a-keyword-in-entire-website","entire")

答案 1 :(得分:1)

试试这个,它基本上通过在网站上搜索关键字或词组来检查是否有谷歌搜索结果:

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在搜索结果中使用类名“g”表示特定搜索结果页的“g”类中最多有10个项目,如果没有显示结果则没有“g”类这意味着没有可计算的物品。