Link to home
Start Free TrialLog in
Avatar of Marcusw
Marcusw

asked on

Retrieve Search Engine Results

Is there a way using VB6 to extract information from a serach engine.

I run a number of website that i currently manually have to check various page ranks and such on.

for instance i may run the query link: http://www.direct-marketing-lists.co.uk on google

what i need to do is extract the count result of 8 from the top right of the screen.

I also wish to do this with a number of different search engines

any ideas?
Avatar of zzzzzooc
zzzzzooc

You can use an HTMLDocument object with a hard-coded search-query link to retrieve the HTML. Then simply parse it. Example of getting the first 8 results from Google below.

Project -> References -> "Microsoft HTML Object Library"

Form1:
-------------------

Private Const sURLGoogle As String = "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=REPLACEME&btnG=Google+Search"
Private Sub Form_Load()
    Dim sResults As Collection, iLoop As Integer
    Call SearchGoogle("example", sResults)
    For iLoop = 1 To 8
        MsgBox sResults(iLoop)
    Next iLoop
End Sub
Private Sub SearchGoogle(ByVal sQuery As String, ByRef sResults As Collection)
    Dim hDoc1 As New HTMLDocument, hDoc2 As New HTMLDocument
    Dim hCol1 As IHTMLElementCollection, objTemp As Object
    Set hDoc1 = hDoc2.createDocumentFromUrl(Replace(sURLGoogle, "REPLACEME", sQuery), "null")
    Do Until hDoc1.readyState = "complete"
        DoEvents
    Loop
    Set hCol1 = hDoc1.All.tags("p")
    Set sResults = New Collection
    For Each objTemp In hCol1
        If sResults.Count <> 8 Then
            Call sResults.Add(objTemp.outerText)
        End If
    Next objTemp
End Sub


Avatar of Marcusw

ASKER

i think you have misunderstood what i was trying to do.

I am only after the actual number.

I do not need the results, just how many results the are in total
You asked for an idea and I gave you one.

The below will get you the 3rd table within the search-page (which contains the number of results) and return the text to the function. You can parse it (mid/instr/left/right/whatever) for more accurate numbers. To implement other search engines, you'll need to search MSDN for help about the HTMLDocument architecture to better understand how to deal with elements and their collections/properties/methods. After that, it should be simple to view the sources of the search-engines to find out where's what.


Form1:
------------

Option Explicit

Private Const sURLGoogle As String = "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=REPLACEME&btnG=Google+Search"
Private Sub Form_Load()
    Dim sReturn As String
    sReturn = SearchGoogle("myquery")
    MsgBox sReturn
End Sub
Private Function SearchGoogle(ByVal sQuery As String) As String
    Dim hDoc1 As New HTMLDocument, hDoc2 As New HTMLDocument, hTable As New HTMLTable
    Set hDoc1 = hDoc2.createDocumentFromUrl(Replace(sURLGoogle, "REPLACEME", sQuery), "null")
    Do Until hDoc1.readyState = "complete"
        DoEvents
    Loop
    Set hTable = hDoc1.All.tags("table")(3)
    SearchGoogle = hTable.outerText
End Function
ASKER CERTIFIED SOLUTION
Avatar of imarshad
imarshad
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial