Program Yahoo! with Python



Program Yahoo! with Python

Use the existing Yahoo! library for Python to build applications quickly.

The Yahoo! Developer Network web site (http://developer.yahoo.net) provides a number of tools to help developers build applications with Yahoo! data. Their Yahoo! Search Web Services software development kit (SDK) includes a handy Python library called pYsearch that does most of the heavy lifting (making requests and parsing responses) for you. This means you have a simple set of commands to learn and interact with, rather than having to take the time to learn how Yahoo! request URLs should be formatted. In exchange for this ease of use, you'll need to spend a few minutes installing the library.

Download the SDK (at http://developer.yahoo.net/download) and unzip its contents to install the pYsearch library. From a command prompt, change directories to the newly unzipped SDK files and then change into the /python/pYsearch directory. In this directory, you'll find setup.py, which you'll need Python 2.2.3 to run. If you can't install a newer version, or if you'd rather not, there's a quick way to make the library compatible with older versions of Python. Just add the following code to setup.py, toward the top of the script:

# add for Python versions that don't understand "classifiers"
import sys
if sys.version < '2.2.3':
	from distutils.dist import DistributionMetadata
	DistributionMetadata.classifiers = None
	DistributionMetadata.download_url = None

Once setup.py is ready to go for your system, install it from the command prompt with these two commands:

python setup.py build
python setup.py install

If everything works as it should, the pYsearch library will now be available to your Python scripts.

The Code

This simple Python script uses the pYsearch library to return Yahoo! Web Search responses. Save this code to a file called yahoo_search.py and be sure to add your own unique application ID:

#!/usr/bin/python
# yahoo_search.py
# A quick Yahoo! Web Search script using Yahoo!'s
# pYsearch library availble in the Y!WS SDK
# [http://developer.yahoo.net/download/]
# Usage: python yahoo_search.py <Query>

import sys, string, codecs

# Use the pYsearch functions
from yahoo.search import webservices

# Grab the query from the command line
if sys.argv[1:]:
query = sys.argv[1]
else:
sys.exit('Usage: python yahoo_search.py <query>')

# Include your unique application ID
appID = 'insert your app ID'

# Query Yahoo!
search = webservices.create_search('web', appID)
search.language = "en"
search.results = 10
search.start = 1
search.query = query

# Parse the results
try:

    results = search.parse_results()
except Exception, err:
	print "Got an error: ", err
	sys.exit(1)

# Tell standard output to handle utf-8 encoding
sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)

# Start counter
count = search.start

# Print out the results
for result in results:
	print "%s. %s\n%s\n%s\n\n" % (count, result.Title, result.Summary,
result.Url) count += 1

The key to using the pYsearch library is importing the webservices module at the top of the script. From there, the script calls the create_search function, sets some parameters, and uses parse_results to get the entire Yahoo! response. And the last print command formats the response and displays it for the user.

The web search type is specified in the create_search function, but keep in mind that you could use any of Yahoo!'s search services here.

Running the Hack

Run the script on the command line:

python yahoo_search.py insert word

And, as usual, enclose multiple words in quotation marks:

python yahoo_search.py "insert multiword phrase"

Figure shows the results for "learning Python".

Python is known as a language that can assemble complex applications quickly; if you're planning to integrate Yahoo! data with a Python application, it's that much faster because most of the hard work is done for you already in the pYsearch library.

Yahoo! Search results for "learning Python"