【问题标题】:Python URL downloadPython 网址下载
【发布时间】:2010-09-03 16:07:59
【问题描述】:

下面的代码返回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()

【问题讨论】:

    标签: python url


    【解决方案1】:

    您必须从fetch_quote 函数中显式地return data。像这样的:

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

    在没有显式返回语句的情况下,Python 返回 None,这就是您所看到的。

    【讨论】:

      【解决方案2】:

      你的方法没有明确地return 任何东西,所以它@98​​7654322@ None

      【讨论】:

        猜你喜欢
        • 2016-01-03
        • 1970-01-01
        • 2015-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 2017-11-12
        • 2018-11-20
        相关资源
        最近更新 更多