Quantcast
Channel: HTML & Web Page Design – Ask Dave Taylor
Viewing all articles
Browse latest Browse all 46

Add a Google News Search Box to your Site?

$
0
0
add google news gnews search box your web page site html

If you’re so inclined, you can certainly go to Google News, crack open the source code and figure out exactly what’s going on with the search form. That’s the hard way because Google has a lot of smarts in the search boxes, including hints and matches as you type. Those are nice, but complicated.

Instead, I’m a fan of reverse engineering the problem: Figure out the data needed to get the search results page to show matches for a given search and you can unwind the results and identify exactly what’s needed.

To start, therefore, you need to pop onto Google News and do a search. Let’s do a quick news search for actor Chris Evans.

On the Gnews home page, there’s a nice big search box on the top:

google news search box

Type in “Chris Evans”, click on the magnifying glass button and you’ll get the results of the search:

chris evans search results, google news gnews

The important thing here is the URL, actually, not any of the code on the page itself.

A copy and paste reveals this is the full URL of the search results page:

https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q=chris+evans&oq=chris+evans&gs_l=news-cc.3..43j0l9j43i53.24045.25128.0.28106.11.7.0.4.4.0.119.554.6j1.7.0…0.0…1ac.1.oFS9W6Fg-to

We can unwrap that to the following:

  • hl=en
  • gl=us
  • tbm=nws
  • authuser=o
  • q=chris+evans
  • oq=chris+evans
  • gs_l=really long sequence of stuff

With a bit of experimentation, it turns out that you only need two of these fields: “tbm” to specify that it’s a news query, and “q” to specify the query string. That makes the URL a lot less overwhelming:

https://www.google.com/search?tbm=nws&q=chris+evans

And it gives us everything we need to create a quick and easy HTML search form:

<form method=”get” action=”https://www.google.com/search”>
<input type=”text” name=”q” />
<input type=”hidden” name=”tbm” value=”nws” />
<input type=”submit” value=”search Google News” />
</form>

Let’s give that a whirl, shall we? Here’s the search box, live:

Did you try it? Not glamorous in appearance, for sure, but functional!

Now let’s pretty it up just a bit so it’ll be something you’d want to actually add to your site:

<form method=”get” action=”https://www.google.com/search” style=”border:1px solid black;border-radius:5px;padding:7px;background:#eef;”>
Search Google News: <input type=”text” name=”q” size=”40″ style=”border:1px solid black;padding:3px;border-radius:4px; ” />
<input type=”hidden” name=”tbm” value=”nws” />
<input type=”submit” value=”Go!” style=”border-radius:4px;” />
</form>

And the result, with its groovy blue background:

Search Google News:

That’s it! Just copy the above HTML and make sure you paste it into your page editor in Source or Raw or HTML mode.

And, finally, since you know the short URL format, you can always pre-build any search pattern you’d like by replacing “chris+evans” with something else. Like “captain+america”:

Just make sure you remember to use the “+” to replace any spaces in the search phrase. Then you can also just sprinkle them into your regular text too, like this: Get breaking news about Captain America with a click!

The post Add a Google News Search Box to your Site? appeared first on Ask Dave Taylor.


Viewing all articles
Browse latest Browse all 46

Trending Articles