I am trying to use a programming language to search google or another specified search engine. I would like to use windows cmd prompt to do so because the specified programming language has a simple command to access the cmd prompt.
Any ideas on how to search google from cmd prompt?
Simply type this on the command-line or in the run command and it will open your default browser to let Google search for SEARCHTERM
:
start www.google.com/search?q=SEARCHTERM
Note that you have to replace whitespaces with pluses, e.g.
start www.google.com/search?q=Use+cmd+prompt+to+search+a+word+on+google+or+other+search+engine
Alternatively you could also put this command in a batch file:
@start www.google.com/search?q=%1+%2+%3+%4+%5+%6+%7+%8+%9
I created a Batch file 'g.bat' and added it to my PATH. It looks like this:
start www.google.co.uk/search?q=%1+%2+%3+%4+%5
Supports up to 5 words (of course you can add more). Now I can search from CMD or start by typing "g query"
Edit: Credit to mrt for the inspiration
You can write in the ps1 file:
function googleSearch{start www.google.com/search?q=$args}
set-alias g googleSearch
then restart your powershell,
g whatwhatwhat
I suppose you could use wget from the command line.
wget -U "Firefox/3.0.15" http://www.google.com/search?q=SEARCH+TERMS+HERE -O result.html -q
Or -O- -q
to output to stdout. Scraping the results from the html will be a different matter entirely, though.
If you do get wget, might as well get grep as well, or just get all of GnuWin32 as it's quite useful. Then you can do stuff like this:
wget -U "Firefox/3.0.15" "http://www.google.com/search?q=wget+google+search" -O- -q 2>&1 | grep -E -o -e "<cite>[^<]+</cite>" | sed -r -e "s/<[^>]+>//g"
... to get the URL of the first link from a Google search. The sky's the limit. Get creative.
(Output of the example command above: isaksen.biz/blog/?p=470
)
If you want to display the first title plus the first URL, it gets a little more complicated.
@echo off
setlocal enabledelayedexpansion
set search=%1 %2 %3 %4 %5 %6 %7 %8 %9
for /l %%a in (1,1,8) do if "!search:~-1!"==" " set search=!search:~0,-1!
set search=%search: =+%
wget -U "Firefox/3.0.15" "http://www.google.com/search?q=%search%" -O search.html -q 2>NUL
for /f "tokens=*" %%I in ('grep -P -o "<h3 class=.*?</h3>" search.html ^| sed -r -e "s/<[^>]+>//g"') do (
echo %%I
goto next
)
:next
set /p I="http://"<NUL
for /f "tokens=*" %%I in ('grep -E -o -e "<cite>[^<]+</cite>" search.html ^| sed -r -e "s/<[^>]+>//g"') do (
echo %%I
del /q search.html
goto :EOF
)
Usage: search.bat up to 9 search terms here
Example:
C:\Users\me\Desktop>search command line google
googlecl - Command line tools for the Google Data APIs - Google ...
http://goosh.org/
C:\Users\me\Desktop>
easy.
@echo off
color a
setlocal ENABLEDELAYEDEXPANSION
echo Google Batch
echo Made By GenoSans
start https://discord.gg/WwRtbBe
timeout -t 5 /nobreak
:a
cls
echo ,,
echo .g8'''bgd `7MM
echo .dP' `M MM
echo dM' ` ,pW'Wq. ,pW'Wq. .P'Ybmmm MM .gP'Ya
echo MM 6W' `Wb 6W' `Wb :MI I8 MM ,M' Yb
echo MM. `7MMF'8M M8 8M M8 WmmmP' MM 8M''''''
echo `Mb. MM YA. ,A9 YA. ,A9 8M MM YM. ,
echo `'bmmmdPY `Ybmd9' `Ybmd9' YMMMMMb .JMML.`Mbmmd'
echo 6' dP
echo Ybmmmd'
echo.
set /p s=Search:
set word=+
set str=%s%
set str=%str: =!word!%
start http://www.google.com/search?q=%str%
goto a
If u are using java then it is damn easy...
// Java program to illustrate
// executing commands on cmd prompt
class NewClass
{
public static void main(String[] args)
{
try
{
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"start www.google.com/search?q=**search here instead of space use '+'**\"");
}
catch (Exception e)
{
System.out.println("HEY Buddy ! U r Doing Something Wrong ");
e.printStackTrace();
}
}
}
Hope this works! Kudos.
Maybe this?
@echo off
:start
cls
echo.
echo G O O G L E Web Search Version 1.1
echo.
echo Type search terms, and your internet browser will open it.
echo.
set /p Web=
start www.google.com/search?q=%Web%
goto start
Save it as .bat
and boom!