python3获取stock历史信息

urlopen

#python
import json,urllib
from urllib.request import urlopen

url = 'http://api.k780.com'
params = {
  'app' : 'finance.stock_history',
  'symbol' : 'sh601318',
  'date' : '20200512',
  'appkey' : '10003',
  'sign' : 'b59bc3ef6191eb9f747dd4e83c99f2a4',
  'format' : 'json',
}
params = urllib.parse.urlencode(params)

f = urllib.request.urlopen('%s?%s' % (url, params))
nowapi_call = f.read()
#print nowapi_call
a_result = json.loads(nowapi_call)
if a_result:
  if a_result['success'] != '0':
    print(a_result['result'])
    print("totline : ",a_result['result']['totline'])
    print("disline : ",a_result['result']['disline'])
    print("page : ",a_result['result']['page'])
    print("name : ",a_result['result']['lists']['2020-05-12']['sname'])
    print("open : ",a_result['result']['lists']['2020-05-12']['open_price'])
    print("close : ",a_result['result']['lists']['2020-05-12']['last_price'])
    print("high : ",a_result['result']['lists']['2020-05-12']['high_price'])
    print("low : ",a_result['result']['lists']['2020-05-12']['low_price'])
    print("volume : ",a_result['result']['lists']['2020-05-12']['volume'])
  else:
    print(a_result['msgid'],' ',+a_result['msg'])
    
else:
  print('Request nowapi fail.')

requests 版本

#python
import requests
import json

url = 'http://api.k780.com'
params = {
  'app' : 'finance.stock_history',
  'symbol' : 'sh601318',
  'date' : '20200513',
  'appkey' : '10003',
  'sign' : 'b59bc3ef6191eb9f747dd4e83c99f2a4',
  'format' : 'json',
}

f = requests.post(url, data = params)
print(f.text)
if f.status_code == 200:
    result = json.loads(f.text)
    if result['success'] != '0':
        print(result['result'])
        print("totline : ",result['result']['totline'])
        print("disline : ",result['result']['disline'])
        print("page : ",result['result']['page'])
        print("name : ",result['result']['lists']['2020-05-13']['sname'])
        print("open : ",result['result']['lists']['2020-05-13']['open_price'])
        print("close : ",result['result']['lists']['2020-05-13']['last_price'])
        print("high : ",result['result']['lists']['2020-05-13']['high_price'])
        print("low : ",result['result']['lists']['2020-05-13']['low_price'])
        print("volume : ",result['result']['lists']['2020-05-13']['volume'])
    else:
        print(result['msgid'],' ',+result['msg'])
else:
    print('Request nowapi fail.')

你可能感兴趣的:(python)