Python下载URL
下面的代码返回的是 none
。我该怎么修复它呢?我正在使用 Python 2.6。
import urllib
URL = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1t1v&e=.csv"
symbols = ('GGP', 'JPM', 'AIG', 'AMZN','GGP', 'JPM', 'AIG', 'AMZN')
#symbols = ('GGP')
def fetch_quote(symbols):
url = URL % '+'.join(symbols)
fp = urllib.urlopen(url)
try:
data = fp.read()
finally:
fp.close()
def main():
data_fp = fetch_quote(symbols)
# print data_fp
if __name__ =='__main__':
main()
2 个回答
2
你的方法没有明确地 返回
任何东西,所以它 返回
的就是 None
。
4
你需要在 fetch_quote
函数里明确地 return
你想要的数据。可以这样写:
def fetch_quote(symbols):
url = URL % '+'.join(symbols)
fp = urllib.urlopen(url)
try:
data = fp.read()
finally:
fp.close()
return data # <======== Return
如果没有明确的返回语句,Python 默认会返回 None
,这就是你现在看到的结果。