开发者

Python URL download

开发者 https://www.devze.com 2023-01-14 23:06 出处:网络
The code below returns none. How can I fix it? I\'m using Python 2.6. import urllib URL = \"http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1t1v&e=.csv\"

The code below returns none. How can I fix it? I'm using 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', 'A开发者_C百科MZN')
#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()


You have to explicitly return the data from fetch_quote function. Something like this:

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

In the absence of an explicit return statement Python returns None which is what you are seeing.


Your method doesn't explicitly return anything, so it returns None

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号