基本上是尝试修改本教程的货币而不是股票: http://pythonprogramming.net/advanced-matplotlib-graphing-charting-tutorial/
使用我当前的代码,我得到:
Error: main loop can only concatenate tuple (not "str") to tuple
代码:
import urllib2
import time
CurrencysToPull = 'audusd','eurusd','usdcad'
def pullData(Currency):
try:
fileline = Currency+'.txt'
urlToVisit = 'http://finance.yahoo.com/echarts?s=Currency=X#{"allowChartStacking":true}/'+Currency+'/chartdata;type=quote:range=1y/csv'
sourcecode = urllib2.urlopen(urlToVisit).read()
splitSource = sourcecode.split('n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'valuse' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'n'
saveFile.write(lineToWrite)
print 'Pulled', Currency
print 'sleeping'
time.sleep(1)
except Exception,(e):
print('main loop'), str(e)
for eachStock in CurrencysToPull:
pullData(CurrencysToPull)
你正在将CurrencysToPull
元组传递给你的函数:
for eachStock in CurrencysToPull:
pullData(CurrencysToPull)
然后尝试将字符串连接到:
fileline = Currency+'.txt'
您可能打算传入eachStock
:
for eachStock in CurrencysToPull:
pullData(eachStock)
错误在此行中: fileline = Currency+'.txt'
货币是一个元组,.txt是一个字符串
在你的for循环中,你传递的是CurrencysToPull而不是每个股票。它应该是:
for eachStock in CurrencysToPull:
在异常处理中使用回溯可以获得有关错误的更好信息。
except Exception,(e):
print('main loop'), str(e)
print(traceback.format_exc())