Python URL download

前端 未结 2 637
夕颜
夕颜 2021-01-25 19:35

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&         


        
2条回答
  •  心在旅途
    2021-01-25 20:35

    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.

提交回复
热议问题