Welcome, guest | Sign In | My Account | Store | Cart

The web has a lot of resources where on can get answers to questions such as wiki Answers and Yahoo! answers. Of this Y! answers provide a quick and easy way to query it by using Yahoo's own query language, YQL. This recipe demonstrates using Yahoo! answers, YQL and Python to get an answer to your query on the command line.

Python, 50 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Use YQL to answer questions with Yahoo! answers and python-yql
# Download Python-YQL from http://python-yql.org
import yql
import sys
import optparse

def prepare_answer(answer):
    attrs = map(answer.get, ['Subject', 'ChosenAnswer', 'Date', 'UserNick', 'ChosenAnswererNick', 'Link'])
    prefixes = ('Question', 'Answer', 'Answered On', 'Asked By', 'Answered By', 'Link')
    lines = []

    for x in range(len(attrs)):
        val = attrs[x]
        if val != None:
            lines.append(': '.join((prefixes[x].ljust(15), val)))

    return '\n'.join(lines)
    
def fetch_answer(*question, **kwargs):
    query="select * from answers.search where query='%s'"
    y = yql.Public()
    print 'You asked: ',' '.join(*question)
    result = y.execute(query % ' '.join(*question))

    if len(result.rows)==0:
        print 'No answers found!'
        return

    limit = kwargs['limit']
    
    if limit>0:
        results = result.rows[:limit]
    else:
        results = result.rows
        
    for item in results:
        if item.get('type')==u'Answered':
            print prepare_answer(item)
            print '_'*82
            
if __name__ == "__main__":
    if len(sys.argv)<2:
        sys.argv.append('-h')
        
    parser = optparse.OptionParser()
    parser.add_option('-n','--num',type=int,dest='nresults',default=0,help='Maximum number of results')
    options, args = parser.parse_args()
    nresults = options.__dict__['nresults']
    
    fetch_answer(args, limit=nresults)

It is very easy to get answer to a question using this script.

Usage

$ python yql_answers.py Usage: yql_answers.py [options]

Options: -h, --help show this help message and exit -n NRESULTS, --num=NRESULTS Maximum number of results

Example query

$ python yql_answers.py -n 2 Who first landed on the moon You asked: Who first landed on the moon Question : The men who first landed on the moon planted a flag Is it possible to still see it ? Answer : Yes but the conspiracy theoriests will just argue that the photos are fake. Answered On : 2010-08-25 12:01:43 Asked By : bob Answered By : Paul Link : http://answers.yahoo.com/question/?qid=20100825120143AAIMV7B


Question : what was the name of the guy who first landed on the moon? Answer : Neil Armstrong. July, 1969. "One small step for man; one giant leap for mankind." I was 16 years old and stayed up all night to watch it on tv. Answered On : 2007-03-15 18:01:29 Asked By : b_ballinmark Answered By : Susan K Link : http://answers.yahoo.com/question/?qid=20070315180129AAmiWSl