2010-09-03 9 views
1

Le code ci-dessous renvoie none. Comment puis-je le réparer? J'utilise Python 2.6.Python URL télécharger

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() 

Répondre

4

Vous devez explicitement return la data de fetch_quote fonction. Quelque chose comme ceci:

def fetch_quote(symbols): 
    url = URL % '+'.join(symbols) 
    fp = urllib.urlopen(url) 
    try: 
     data = fp.read() 
    finally: 
     fp.close() 
    return data # <======== Return 

En l'absence d'une déclaration de retour explicite retourne Python None qui est ce que vous voyez.

+0

J'ai testé ce correctif, ça marche. "GGP", 14.65, "12:04 pm", 689816 ... – msw

+0

merci, l'a fixé !!!! – Merlin

2

Votre méthode ne explicitement return quoi que ce soit, il returnsNone

+0

Comment puis-je corriger, merci – Merlin

+4

Uhm, retourner quelque chose? – Santa

Questions connexes