Build your own personal, task-specific Google search form.
If you want to do a simple search with Google, you don’t need anything but the standard Simple Search form (the Google home page). But if you want to craft specific Google searches that you’ll be using on a regular basis or providing for others, you can simply put together your own personalized search form.
Start with your garden-variety Google search form; something like this will do nicely:
<!-- Search Google --> <form method="get" action="http://www.google.com/search"> <input type="text" name="q" size=31 maxlength=255 value=""> <input type="submit" name="sa" value="Search Google"> </form> <!-- Search Google -->
This is a very simple search form. It takes your query and sends it directly to Google, adding nothing to it. But you can embed some variables to alter your search as needed. You can do this in two ways: via hidden variables or by adding more input to your form.
As long as you know how to identify a search option in Google, you can add it to your search form via a hidden variable. The fact that it’s hidden just means that form users will not be able to alter it. They won’t even be able to see it unless they take a look at the source code. Let’s take a look at a few examples.
Tip
While it’s perfectly legal HTML to put your hidden
variables anywhere between the opening and closing
<form>
tags, it’s rather
tidy and useful to keep them all together after all the visible form
fields.
- File Type
As the name suggests, file type specifies filtering your results by a particular file type (e.g., Word
.doc
, Adobe.pdf
, PowerPoint.ppt
, plain text.txt
). Add a PowerPoint file type filter, for example, to your search form, like so:<input type="hidden" name="as_filetype" value="PPT">
- Site Search
Narrows your search to specific sites. While a suffix like
.com
will work just fine, something more fine-grained like theexample.com
domain is probably better suited:<input type="hidden" name="as_sitesearch" value="example.com">
- URL Component
Specifies a particular path component to look for in URLs. This can include a domain name but doesn’t have to. The following tries to tease out documentation in your result set:
<input type="hidden" name="hq" value="inurl:docs">
- Date Range
Narrows your search to pages indexed within the stated number of months. Acceptable values are between 1 and 12. Restricting our results to items indexed only within the last seven months is just a matter of adding:
<input type="hidden" name="as_qdr" value="m7">
- Number of Results
Indicates the number of results that you’d like appearing on each page, specified as a value of
num
between 1 and 100; the following asks for 50 per page:<input type="hidden" name="num" value="50">
What would you use this for? If you’re regularly looking for an easy way to create a search engine that finds certain file types in a certain place, this works really well. If this is a one-time search, you can always just hack the results URL ["Understanding Google URLs” earlier in this chapter], tacking the variables and their associated values on to the URL of the results page.
The site tompeters.com (http://www.tompeters.com) contains several
PowerPoint (.ppt
) files. If you want to find
just the PowerPoint files on their site, you’d have
to figure out how their site search engine works or pester them into
adding a file type search option. But you can put together your own
search form that finds PowerPoint presentations on the tompeters.com
site.
Tip
Even though you’re creating a handy search form this way, you’re still resting on the assumption that Google’s indexed most or all of the site that you’re searching. Until you know otherwise, assume that any search results Google gives you are incomplete.
Your form looks something like:
<!-- Search Google for tompeters.com PowerPoints --> <form method="get" action="http://www.google.com/search"> <input type="text" name="q" size=31 maxlength=255 value=""> <input type="submit" name="sa" value="Search Google"><input type="hidden" name="as_filetype" value="ppt">
<input type="hidden" name="as_sitesearch" value="tompeters.com">
<input type="hidden" name="num" value="100">
</form> <!-- Search Google for tompeters.com PowerPoints -->
Using hidden variables is handy when you want to search for one particular thing all the time. But if you want to be flexible in what you’re searching for, creating an alternate form is the way to go.
Some variables best stay hidden; however, for other options, you can let your form users be much more flexible.
Let’s go back to the previous example. You want to let your users search for PowerPoint files, but you also want them to be able to search for Excel files and Microsoft Word files. In addition, you want them to be able to search tompeters.com, the State of California, or the Library of Congress. There are obviously various ways to do this user-interface-wise; this example uses a couple of simple pull-down menus:
<!-- Custom Google Search Form--> <form method="get" action="http://www.google.com/search"> <input type="text" name="q" size=31 maxlength=255 value=""><br />
Search for file type:
<select name="as_filetype">
<option value="ppt">PowerPoint</option>
<option value="xls">Excel</option>
<option value="doc">Word</option>
</select>
<br />
Search site:
<select name="as_sitesearch">
<option value="tompeters.com">TomPeters.com</option>
<option value="state.ca.us">State of California</option>
<option value="loc.gov">The Library of Congress</option>
</select>
<input type="hidden" name="num" value="100"> <input type="submit" value="Search Google"> </form> <!-- Custom Google Search Form-->
FaganFinder (http://www.faganfinder.com/engines/google.shtml) is a wonderful example of a thoroughly customized form.
Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.