in django, python

Integrating Yahoo Search in a Django site in 5 easy steps

I have been experimenting with various search options for the site for a while. Google Custom Search is nice and very fast, but the number of ads appearing in the search result page makes it difficult for users to separate result items from ads. (Update: I am sticking with Google Custom Search until I figure out how to get Yahoo search to present proper excerpts).

I am a fast reader which comes with the tradeoff of missing important information sometimes. Skimming through the terms for using the API I was a bit disappointed at first. That was because I was only reading the first column in the table that lists the previous terms of use. Oh well. The second column that lists the current restrictions (or rather lack of restrictions) makes it clear that Yahoo search is very easy to get started with. It is almost so that you start wondering where Yahoo will make money from providing a service like that.

Anyway, here are the five simple steps to get Yahoo search integrated in your Django site:

1. Get an API Key

…or “application ID” as Yahoo calls it. Visit this page to sign up for an API key.

2. Install the pYsearch library

Download pYsearch from Sourceforge to your computer (here is the direct link to the package), and:

tar xvf pYsearch-3.1.tar.gz

cd pYsearch

sudo python build

sudo python install

This should install pYsearch somewhere on your Python path.

3. Set up URL-pattern and view method

In your add a URL-pattern to pick up search requests. We’ll use queries like /search/?q=myquery:

url(r'^search/$', '', name='site_search'),

Set up the view method in your application’s (make sure the search query is encoded to utf-8 to enable characters outside ISO-8859-1 in the query parameter):

from import WebSearch

def search(request):
	query = request.GET.get("q", "").encode("utf-8")

	if len(query) > 0:

		#Call yahoo!
		api_key = "[your api key]"
		srch = WebSearch(api_key) = "" #restrict to your own site
		srch.query = query
		srch.results = 50
		result = srch.parse_results() #puts all result items into a dict

	return render_to_response('search/search.html', locals())

4. And now the search form and result template

Add the search field and form somewhere on your site:

<form action="/search/" method="GET">
	<label for="q"">Find:</label>
	<input type="text" name="q" accesskey="4" size="30" id="q" value="{{query}}">
	<input type="submit" value="Search">

The base.html sets up the basic web page. This template lives in myapp/templates/search/search.html

{% extends 'base.html' %}
{% block content %}
	{% if result.results %}
        <ul class="searchresult">
	{% for item in result %}
           <h2><a href="{{item.Url}}">{{item.Title}}</a></h2>
           <p>{{item.Summary|cut:" ..."}}...</p>
        {% endfor %}
{% else %}
	<li>Check your spelling.</li>
	<li>Try similar words.</li>
{% endif %}
{% endblock %}

5. There is no step 5!

That’s it! Your site should now have a nice search engine. Some issues you may encounter:

  1. Yahoo does not seem to have indexed all of yet. This means that the result set will be limited. No PDF documents seem to have been included (searching for “V├Ągledningen 24-timmarswebben” does not return the PDF document even though it has many links on the site). It would be nice to be able to see how much of the site that the search engine knows about.
  2. The summary text for each page seems to be the same (including hidden skiplink text). This may be my fault as I haven’t provided meta description elements yet. I have added som CSS classnames (robots-nocontent) to navigation elements to help the search engine decide on what should be included and what should be skipped. I had expected that the summary would contain a phrase close to the query term instead of text from the top of the page.

Write a Comment here on the real web



  • Django 101 | KomunitasWeb January 26, 2009

    […] Integrating Yahoo Search in a Django site in 5 easy steps (Desember 2008) […]

  • Wait till I come! » Blog Archive » TTMMHTM: Was it a good day, food info, scripting enabled video and twitter January 26, 2009

    […] A cool tutorial on integrating Django and BOSS […]

  • links for 2009-01-05 « Mandarine January 26, 2009

    […] Integrating Yahoo BOSS Search in a Django site in 5 easy steps – Peter Krantz (tags: tutorial search python yahoo django) […]