2017-08-18 12 views
0
from googlefinance import getQuotes 

print(getQuotes("NSE:M\&MFIN"),) 

アンパサンドはコードとして扱われていますが、テキストとして扱いたいと思います。私は不正な要求の例外を取得:このアンパサンドをテキストとして扱うにはどうすればよいですか?

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/.../site-packages/googlefinance/__init__.py", line 70, in getQuotes 
    content = json.loads(request(symbols)) 
    File "/.../site-packages/googlefinance/__init__.py", line 33, in request 
    resp = urlopen(req) 
    File "/.../urllib/request.py", line 223, in urlopen 
    return opener.open(url, data, timeout) 
    File "/.../urllib/request.py", line 532, in open 
    response = meth(req, response) 
    File "/.../urllib/request.py", line 642, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/.../urllib/request.py", line 570, in error 
    return self._call_chain(*args) 
    File "/.../urllib/request.py", line 504, in _call_chain 
    result = func(*args) 
    File "/.../urllib/request.py", line 650, in http_error_default 
    raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 400: Bad Request 

私は(\を使用して)それを脱出しようとしたが、これはどちらか動作しませんでした。

このアンパサンドをテキストとして扱うにはどうすればよいですか?

+1

「NSE%3AM%26MFIN'」としてエスケープするとうまくいきます... –

答えて

0

ライブラリはそれだけでthe source codeを参照し、適切なエンコードせずにURLにシンボルを追加することで、むしろナイーブです:

def buildUrl(symbols): 
    symbol_list = ','.join([symbol for symbol in symbols]) 
    # a deprecated but still active & correct api 
    return 'http://finance.google.com/finance/info?client=ig&q=' \ 
     + symbol_list 

あなたが使用して、手動でフロントまで引用してこの問題を回避することができますurllib.parse.quote() function

from urllib.parse import quote 

print(getQuotes(quote("NSE:M&MFIN"))) 

デモ:

>>> from googlefinance import getQuotes 
>>> from urllib.parse import quote 
>>> print(getQuotes(quote("NSE:M&MFIN"))) 
[{'ID': '11784956', 'StockSymbol': 'M&MFIN', 'Index': 'NSE', 'LastTradePrice': '416.55', 'LastTradeWithCurrency': '&#8377;416.55', 'LastTradeTime': '3:30PM GMT+5:30', 'LastTradeDateTime': '2017-08-18T15:30:00Z', 'LastTradeDateTimeLong': 'Aug 18, 3:30PM GMT+5:30'}] 
関連する問題