Freebase python -
i tried python example freebase , run in windows , ubuntu machine.
http://mql.freebaseapps.com/ch04.html
import sys # command-line arguments, etc. import simplejson # json encoding. import urllib # uri encoding. import urllib2 # high-level url content fetching. # these constants we'll use. server = 'api.freebase.com' # metaweb server service = '/api/service/mqlread' # metaweb service # compose our mql query python data structure. # query array in case multiple bands share same name. band = sys.argv[1] # desired band, command line. query = [{'type': '/music/artist', # our mql query in python. 'name': band, # place band in query. 'album': [{ 'name': none, # none python's null. 'release_date': none, 'sort': 'release_date' }]}] # put query in envelope envelope = { 'query': query, # query property specifies query. 'escape': false # turns off html escaping. } # these 5 lines key code using mqlread encoded = simplejson.dumps(envelope) # json encode envelope. params = urllib.urlencode({'query':encoded}) # escape request parameters. url ='http://%s%s?%s' % (server,service,params) # url request. f = urllib2.urlopen(url) # open url file. response = simplejson.load(f) # read , json parse response. # check errors , exit message if query failed. if response['code'] != '/api/status/ok': # if not okay... error = response['messages'][0] # first msg object. sys.exit('%s: %s' % (error['code'], error['message'])) # display code,msg. # no errors, handle result result = response['result'] # open response envelope, result. # check number of matching bands if len(result) == 0: sys.exit('unknown band') elif len(result) > 1: print "warning: multiple bands named " + band + ". listing first only." result = result[0] # first band array of matches. if not result['album']: # exit if band has no albums sys.exit(band + ' has no known albums.') album in result['album']: # loop through result albums. name = album['name'] # album name. date = album['release_date'] # release date timestamp or null. if not date: date = '' else: date = ' [%s]' % date[0:4] # 4-digit year in brackets. print "%s%s" % (name, date) # print name , date.
however, did not work. explain why got error?
traceback (most recent call last): file "mql.py", line 29, in <module> f = urllib2.urlopen(url) # open url file. file "c:\python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) file "c:\python27\lib\urllib2.py", line 394, in open response = self._open(req, data) file "c:\python27\lib\urllib2.py", line 412, in _open '_open', req) file "c:\python27\lib\urllib2.py", line 372, in _call_chain result = func(*args) file "c:\python27\lib\urllib2.py", line 1199, in http_open return self.do_open(httplib.httpconnection, req) file "c:\python27\lib\urllib2.py", line 1168, in do_open h.request(req.get_method(), req.get_selector(), req.data, headers) file "c:\python27\lib\httplib.py", line 955, in request self._send_request(method, url, body, headers) file "c:\python27\lib\httplib.py", line 989, in _send_request self.endheaders(body) file "c:\python27\lib\httplib.py", line 951, in endheaders self._send_output(message_body) file "c:\python27\lib\httplib.py", line 811, in _send_output self.send(msg) file "c:\python27\lib\httplib.py", line 773, in send self.connect() file "c:\python27\lib\httplib.py", line 754, in connect self.timeout, self.source_address) file "c:\python27\lib\socket.py", line 562, in create_connection sock.connect(sa)
one more thing, using python 2.7 , don't have special proxy configuration internet setting.
api.freebase.com
has been retired , python client library never updated work new endpoint.
Comments
Post a Comment