Jump to content

Is there a faster way to make a google search script?


Recommended Posts

Hello I was wanting to know if there was a fast way to make a script that will search google for a specific keyword and show how many results came up in an output box?

So lets say I want to search for these keyword phrases:

"autoit rocks" If you type this into google it comes up with about 488 results.

"I love autoit!" It comes up with 218 results.

Is there a way to make a script that will search google for whatever keywords i put in it then give the number of results back in an output box? Or is this too complex because it has to "read" the number of results?

Thanks for any comments or answers/advice.

Link to comment
Share on other sites

Is there a way to make a script that will search google for whatever keywords i put in it then give the number of results back in an output box? Or is this too complex because it has to "read" the number of results?

Yes and sort of yes.

Is there a way ... - sure there is.

Is it too complex. - Not sure if is complexed, but in general, you'll have to:

1. send your request to google

2. read the output.

3. parse your needed string.

4. show it.

Sorry, that the way I know to how to do it.

HTH,

YG

Link to comment
Share on other sites

#include <Array.au3>
#include <HTTP.au3>
Opt('ExpandVarStrings', 1)

Dim $sHost, $sPage, $sEncStr, $sRead
Dim $Sock
Dim $aMatch, $aLinks[10][2]
Dim $iCounter = 0

$sEncStr = _EncodeURLString('"AutoIt Rocks"')
$sHost = 'www.google.com'
$Sock = _HTTPConnect($sHost)

For $iCounter = 0 To 30 Step 10
    $sPage = '/search?q=$sEncStr$&hl=en&start=$iCounter$&sa=N'
    _HTTPGet($sHost, $sPage, $Sock)
    $sRead = _HTTPRead($Sock)
    FileWrite(@ScriptDir & "\temp" & $iCounter & ".txt", $sRead)
    
    $aMatch = StringRegExp($sRead, '(?i)(?=<ol)<ol.*?>(.*?)</ol>', 1)

    If IsArray($aMatch) Then 
        $aMatch = StringRegExp($aMatch[0], '(?i)<h3.*?><a.*?href="([^"]*)".*?>([^<]*)', 3)
        If IsArray($aMatch) Then
            Local $iLinks = UBound($aMatch)/2
            
            For $i = 0 To $iLinks-1
                $aLinks[$i][0] = $aMatch[$i*2]
                $aLinks[$i][1] = StringRegExpReplace($aMatch[$i*2+1], '&quot;', '"')
            Next
            
            _ArrayDisplay($aLinks)
        EndIf
    EndIf
Next

Exit


Func _EncodeURLString($sString)
    Local $avAsc[256] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, _
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
                         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                         
    Local $sEncodedString = "", $cChar, $iAsc
    
    For $i = 1 To StringLen($sString)
        $cChar = StringMid($sString, $i, 1)
        $iAsc = Asc($cChar)
        
        If $avAsc[$iAsc] Then
            $sEncodedString &= $cChar
        Else
            $sEncodedString &= '%' & Hex($iAsc, 2)
        EndIf
    Next
    
    Return $sEncodedString
EndFunc

You need to change the HTTP.au3 library to keep the connection, otherwise you'll need to reconnect every loop.

Change the line:

$command &= "Connection: close"&@CRLF

to:

$command &= "Connection: Keep-Alive"&@CRLF
Link to comment
Share on other sites

If you haven't learned StringRegExp yet, you really should. It is a little confusing to start out with, but once you get it, you can parse data much faster and easier. No more trimming the string. A great tool that I have found is called Expresso. And the best part is that it is free. Just register (free) and put in your serial number and you're off.

Link to comment
Share on other sites

#include <Array.au3>
#include <HTTP.au3>
Opt('ExpandVarStrings', 1)

Dim $sHost, $sPage, $sEncStr, $sRead
Dim $Sock
Dim $aMatch, $aLinks[10][2]
Dim $iCounter = 0

$sEncStr = _EncodeURLString('"AutoIt Rocks"')
$sHost = 'www.google.com'
$Sock = _HTTPConnect($sHost)

For $iCounter = 0 To 30 Step 10
    $sPage = '/search?q=$sEncStr$&hl=en&start=$iCounter$&sa=N'
    _HTTPGet($sHost, $sPage, $Sock)
    $sRead = _HTTPRead($Sock)
    FileWrite(@ScriptDir & "\temp" & $iCounter & ".txt", $sRead)
    
    $aMatch = StringRegExp($sRead, '(?i)(?=<ol)<ol.*?>(.*?)</ol>', 1)

    If IsArray($aMatch) Then 
        $aMatch = StringRegExp($aMatch[0], '(?i)<h3.*?><a.*?href="([^"]*)".*?>([^<]*)', 3)
        If IsArray($aMatch) Then
            Local $iLinks = UBound($aMatch)/2
            
            For $i = 0 To $iLinks-1
                $aLinks[$i][0] = $aMatch[$i*2]
                $aLinks[$i][1] = StringRegExpReplace($aMatch[$i*2+1], '&quot;', '"')
            Next
            
            _ArrayDisplay($aLinks)
        EndIf
    EndIf
Next

Exit


Func _EncodeURLString($sString)
    Local $avAsc[256] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, _
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
                         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                         
    Local $sEncodedString = "", $cChar, $iAsc
    
    For $i = 1 To StringLen($sString)
        $cChar = StringMid($sString, $i, 1)
        $iAsc = Asc($cChar)
        
        If $avAsc[$iAsc] Then
            $sEncodedString &= $cChar
        Else
            $sEncodedString &= '%' & Hex($iAsc, 2)
        EndIf
    Next
    
    Return $sEncodedString
EndFunc

You need to change the HTTP.au3 library to keep the connection, otherwise you'll need to reconnect every loop.

Change the line:

$command &= "Connection: close"&@CRLF

to:

$command &= "Connection: Keep-Alive"&@CRLF

Thank you very much :) i did that in the http.au3 changed to keep alive hm tried to run the script you gave me and it said $sEncSTr=_EncodeURLString is an unknown function name. Am i supposed to make it or was it supposed to be in the http.au3 cuz it's not? hm lol

Link to comment
Share on other sites

Thank you very much :) i did that in the http.au3 changed to keep alive hm tried to run the script you gave me and it said $sEncSTr=_EncodeURLString is an unknown function name. Am i supposed to make it or was it supposed to be in the http.au3 cuz it's not? hm lol

He gave you the function in his code.

Func _EncodeURLString($sString)
    Local $avAsc[256] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, _
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
                         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                         
    Local $sEncodedString = "", $cChar, $iAsc
    
    For $i = 1 To StringLen($sString)
        $cChar = StringMid($sString, $i, 1)
        $iAsc = Asc($cChar)
        
        If $avAsc[$iAsc] Then
            $sEncodedString &= $cChar
        Else
            $sEncodedString &= '%' & Hex($iAsc, 2)
        EndIf
    Next
    
    Return $sEncodedString
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...