import json
import urllib.parse
import urllib.request
def send(query, fmt='json'):
"""send query to yahoo api"""
response = urllib.request.urlopen(__urlbuilder(query, fmt))
data = json.loads(response.read().decode('utf-8'))
return data
def __urlbuilder(yql_query, fmt):
"""urlencode yql query"""
base_url = 'https://query.yahooapis.com/v1/public/yql?'
query = {
'q': yql_query,
'format': fmt,
'env': 'store://datatables.org/alltableswithkeys'
}
url = base_url + urllib.parse.urlencode(query)
return url
if __name__ == '__main__':
data = send(query = 'select * from yahoo.finance.historicaldata where symbol in ("EUR=X") and startDate = "2014-01-01" and endDate = "2014-12-31"')
quotes = data['query']['results']['quote']
print(quotes)
Recommended Posts