Yi Wen Edwin Ang
Yi Wen Edwin Ang

Reputation: 55

How to parse result from YAHOO.Finance.SymbolSuggest.ssCallback in Python

I'm using Python 3 and trying to get a list of possible stock symbols from company name using YAHOO.Finance.SymbolSuggest.ssCallback.

With the below code,

import urllib

yahoo_url = 'http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=apple+inc&region=1&lang=en&callback=YAHOO.Finance.SymbolSuggest.ssCallback'

response = urllib.request.urlopen(yahoo_url)

str_response = response.read().decode('utf-8')

The result is below:

str_response
Out[136]: 'YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"AAPL","Result":[{"symbol":"AAPL","name":"Apple Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"^NY2LAAPL","name":"ICE Leveraged 2x AAPL Index","exch":"NYS","type":"I","exchDisp":"NYSE","typeDisp":"Index"},{"symbol":"AAPL.BA","name":"Apple Inc.","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPL34.SA","name":"Apple Inc.","exch":"SAO","type":"S","exchDisp":"Sao Paolo","typeDisp":"Equity"},{"symbol":"AAPL.MX","name":"Apple Inc.","exch":"MEX","type":"S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"AAPL.MI","name":"APPLE","exch":"MIL","type":"S","exchDisp":"Milan","typeDisp":"Equity"},{"symbol":"AAPLD.BA","name":"APPLE INC","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPLC.BA","name":"APPLE INC","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPL.VI","name":"Apple Inc.","exch":"VIE","type":"S","exchDisp":"Vienna","typeDisp":"Equity"}]}});'

How do I extract only the below segment? And then put into dict?

"Result":[{"symbol":"AAPL","name":"Apple Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"^NY2LAAPL","name":"ICE Leveraged 2x AAPL Index","exch":"NYS","type":"I","exchDisp":"NYSE","typeDisp":"Index"},{"symbol":"AAPL.BA","name":"Apple Inc.","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPL34.SA","name":"Apple Inc.","exch":"SAO","type":"S","exchDisp":"Sao Paolo","typeDisp":"Equity"},{"symbol":"AAPL.MX","name":"Apple Inc.","exch":"MEX","type":"S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"AAPL.MI","name":"APPLE","exch":"MIL","type":"S","exchDisp":"Milan","typeDisp":"Equity"},{"symbol":"AAPLD.BA","name":"APPLE INC","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPLC.BA","name":"APPLE INC","exch":"BUE","type":"S","exchDisp":"Buenos Aires","typeDisp":"Equity"},{"symbol":"AAPL.VI","name":"Apple Inc.","exch":"VIE","type":"S","exchDisp":"Vienna","typeDisp":"Equity"}]

Thank you in advance.

Upvotes: 0

Views: 243

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62373

convert the string to a dict

from ast import literal_eval

result = literal_eval(str_response[39:-2])

print(type(result))
>>> dict

# Result key of interest
Result = result['ResultSet']['Result']

print(Result)

[{'symbol': 'AAPL',
  'name': 'Apple Inc.',
  'exch': 'NMS',
  'type': 'S',
  'exchDisp': 'NASDAQ',
  'typeDisp': 'Equity'},
 {'symbol': '^NY2LAAPL',
  'name': 'ICE Leveraged 2x AAPL Index',
  'exch': 'NYS',
  'type': 'I',
  'exchDisp': 'NYSE',
  'typeDisp': 'Index'},
 {'symbol': 'AAPL.BA',
  'name': 'Apple Inc.',
  'exch': 'BUE',
  'type': 'S',
  'exchDisp': 'Buenos Aires',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPL34.SA',
  'name': 'Apple Inc.',
  'exch': 'SAO',
  'type': 'S',
  'exchDisp': 'Sao Paolo',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPL.MX',
  'name': 'Apple Inc.',
  'exch': 'MEX',
  'type': 'S',
  'exchDisp': 'Mexico',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPL.MI',
  'name': 'APPLE',
  'exch': 'MIL',
  'type': 'S',
  'exchDisp': 'Milan',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPLD.BA',
  'name': 'APPLE INC',
  'exch': 'BUE',
  'type': 'S',
  'exchDisp': 'Buenos Aires',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPLC.BA',
  'name': 'APPLE INC',
  'exch': 'BUE',
  'type': 'S',
  'exchDisp': 'Buenos Aires',
  'typeDisp': 'Equity'},
 {'symbol': 'AAPL.VI',
  'name': 'Apple Inc.',
  'exch': 'VIE',
  'type': 'S',
  'exchDisp': 'Vienna',
  'typeDisp': 'Equity'}]

Upvotes: 2

Related Questions