我在python中运行了这个api并且可以让它更新.csv文件但是当我只有它打印股票时我不知道它在哪里输出这些数据。这是我的代码:
import urllib
import os
import time
# loop that checks stock prices every 20 seconds and adds them to the file
while 1:
# sometimes this program gives me socket errors so if it does skip this itteration of the loop
try:
stocks = urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=JPM+C&f=snl1c6').read()
except IOError:
print ("error reading the socket")
time.sleep(120) #if we don't sleep here loop constently retrys with no delay
continue
print stocks
答案 0 :(得分:2)
有效。应该没有问题。
>>> stocks = urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=JPM+C&f=snl1c6').read()
>>> print stocks
"JPM","JP Morgan Chase &",33.175,"+0.955"
"C","Citigroup, Inc. N",28.87,"+1.12"
为什么要在循环中执行此操作?如果成功,则立即进行另一次查询。
答案 1 :(得分:0)
python yourprogram.py >> outfile
在您的评论中,您说您希望输出转到文件。这将不断附加到outfile
答案 2 :(得分:0)
默认情况下,它会打印到sys.stdout
。查看print
的文档。