Skip to content

Instantly share code, notes, and snippets.

@yangsheng1107
Created March 15, 2016 14:23
Show Gist options
  • Save yangsheng1107/4bd47694ef6a821b2103 to your computer and use it in GitHub Desktop.
Save yangsheng1107/4bd47694ef6a821b2103 to your computer and use it in GitHub Desktop.
TWSE stock with python
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Name: Get Stock Quote from TWSE
# Purpose:
#
# Author: yangsheng
#
# Created: 03/15/2016
# Copyright: (c) yangsheng 2015
# Licence: <your licence>
#-------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import requests
import time
import json
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Parameter Define
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
stocks = ['2881',
'2882',
'0050']
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Function
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
def getStockInfo(quote):
req = requests.session()
req.get('http://mis.twse.com.tw/stock/index.jsp',headers = {'Accept-Language':'zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4'})
#response = req.get('http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch={}&json=1&delay=0&_{}'.format(quote,int(time.time()*1000)))
response = req.get('http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch={}'.format(quote))
jstr = json.loads(response.text)
return jstr
def displayStockInfo(jobj):
for i in range(0, 3, 1):
print "%s %s %.2f %.2f %.2f " % (jobj['msgArray'][i]['ch'] , jobj['msgArray'][i]['n'] , float(jobj['msgArray'][i]['y']), float(jobj['msgArray'][i]['pz']), (float(jobj['msgArray'][i]['pz']) - float(jobj['msgArray'][i]['y'])))
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
# mian function
# = = = = = = = = = = = = = = = = = = = = = = = = = = =
if __name__ == '__main__':
url = '|'.join(['tse_%s.tw' % (stocks[n]) for n in xrange(len(stocks))])
data = getStockInfo(url)
displayStockInfo(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment