1

ystockquote-master を使用して Yahoo Finance API から価格と時価総額を取得するスクリプトを作成しました。非常にシンプルで、私の PC では問題なく動作しますが、友人の 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")
4

1 に答える 1