LoginSignup
1
1

More than 5 years have passed since last update.

Python3から直接YQLのAPIにアクセスしてみる

Posted at
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)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1