python股票接口_使用SINA接口获取实时股票信息
#!/usr/bin/python3
#coding = utf-8
mport ctypes,sys
import socket
import urllib.request
import re
import time
from colorama import init
from termcolor import colored
#在windows下使用Colorama顯示顏色
init()
#同時(shí)請(qǐng)求多只股票
url = ‘http://hq.sinajs.cn/list=sz300711,sz002733,sz002236,sz002415,sz002386’
headers = {}
headers[‘User-Agent’] = ‘Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17’
while True:
request = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(request)
data = resp.read().decode(‘gb2312’)
stocklist = data.split(’\n’)
#print(stocklist)
for stock in stocklist:
if len(stock.strip()) <= 0:
continue
stock = re.findall(r'"(.+?)"', stock)
#print(stock)
items = stock[0].split(',')
if len(items) > 0:
curr = float(items[3]) # current
yend = float(items[2]) # yesterday end
percent = (curr / yend - 1) * 100 # percent
diff = curr - yend # Price difference
disp = '{:8s} | {:>8s} | {:>6.2f}% | {:>6.2f}'.format(items[0],items[3], percent, diff)
if percent == 0.0:
print(colored(disp, 'white'))
elif percent > 0.0:
print(colored(disp, 'red'))
else: # percent < 0.0
print(colored(disp, 'green'))
print('\r\n')
#10s 更新一次
time.sleep(10)
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python股票接口_使用SINA接口获取实时股票信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python numpy读取数据_pyt
- 下一篇: python 用if判断一个数是不是整数