Python URL请求Yahoo API

时间:2013-07-26 02:17:15

标签: python api yahoo finance stock

我编写了这个脚本,使用ystockquote-master来获取Yahoo Finance API的价格和市值。它非常简单,在我的电脑上运行良好,但是当我尝试在我的朋友mac上使用它时,我得到一个错误。这是一个非常长的,所以我会在最后发布它。我一直在努力找出发生了什么,所以我转过身来。背景:初学者到初学者。以下是我的代码片段:

try:
    # py3
    from urllib.request import Request, urlopen
    from urllib.parse import urlencode
except ImportError:
    # py2
    from urllib2 import Request, urlopen
    from urllib import urlencode

def _request(symbol, stat):
    url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
    req = Request(url)
    resp = urlopen(req)
    return str(resp.read().decode('utf-8').strip())
def get_price(symbol):
    return _request(symbol, 'l1')
def get_market_cap(symbol):
    return _request(symbol, 'j1')

i = 0
while i<len(NewSymbolsList):
    results = open("intermediateresults.csv", "a")
    api = [get_price(NewSymbolsList[i]),get_market_cap(NewSymbolsList[i])]
    api = re.sub("\[\'|\'|\]", "", str(api))
    results.write(str(NewSymbolsList[i]) +"," +str(api) +"\n")
    print NewSymbolsList[i], api                  
    i+=1
results.close()

以下是我在朋友Mac上发生的错误:

cd '/Users/JW/Desktop/market cap/' && '/usr/local/bin/pythonw' -t         '   /Users/JW/Desktop/market cap/MarketCap_Scan.py'  && echo Exit status: $? && exit 1
JWs-MacBook-Pro:~ JW$ cd '/Users/JW/Desktop/market cap/' && '/usr/local/bin/pythonw' -t     '/Users/JW/Desktop/market cap/MarketCap_Scan.py'  && echo Exit status: $? && exit 1
Traceback (most recent call last):
  File "/Users/JW/Desktop/market cap/MarketCap_Scan.py", line 33, in <module>
    api = [get_price(NewSymbolsList[i]),get_market_cap(NewSymbolsList[i])]
  File "/Users/JW/Desktop/market cap/MarketCap_Scan.py", line 18, in get_price
    return _request(symbol, 'l1')
  File "/Users/JW/Desktop/market cap/MarketCap_Scan.py", line 15, in _request
    resp = urlopen(req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 442, in error
    result = self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 629, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
JWs-MacBook-Pro:market cap JW$ 

编辑显示我如何准备要迭代的列表,(非常标准我猜,但认为它可能有帮助):

symbols = open("symbolslist.txt")
readsymbols = symbols.read()
NewSymbolsList = readsymbols.split("\n")

1 个答案:

答案 0 :(得分:1)

好的,我们在这里试试吧。我怀疑正面临的问题是源文件中的空白区域(也许是流氓\ r?)。作为补充说明,我介绍了几个with子句上下文管理器,以说明它们用于文件处理的用法。

import re
try:
    # py3
    from urllib.request import Request, urlopen
    from urllib.parse import urlencode
except ImportError:
    # py2
    from urllib2 import Request, urlopen
    from urllib import urlencode

def _request(symbol, stat):
    url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
    req = Request(url)
    resp = urlopen(req)
    return str(resp.read().decode('utf-8').strip())

def get_price(symbol):
    return _request(symbol, 'l1')

def get_market_cap(symbol):
    return _request(symbol, 'j1')

symbols = open("symbolslist.txt")
readsymbols = symbols.read()
NewSymbolsList = readsymbols.split("\n")

with open('symbolslist.txt') as ifp, open('intermediateresults.csv', 'a') as results_fp:
    for row in ifp:
        row = row.strip()
        api = [ get_price(row), get_market_cap(row) ]
        api = re.sub("\[\'|\'|\]", "", str(api))
        results_fp.write(str(row) +"," +str(api) +"\n")
        print row, api

在我的电脑上打印:

Marks-MBP:~ mroberts$ python aa.py
AAAE 0.005, 171K
RENT 22.07, 262.4M