python - How can I treat this ampersand as text? -


from googlefinance import getquotes  print(getquotes("nse:m\&mfin"),) 

the ampersand being treated code want treat text; bad request exception:

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 

i tried escape (using \) did not work either.

how can treat ampersand text?

the library rather naive in appends symbols url without proper encoding, see the source code:

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

you can work around manually quoting front, using urllib.parse.quote() function:

from urllib.parse import quote  print(getquotes(quote("nse:m&mfin"))) 

demo:

>>> googlefinance import getquotes >>> 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'}] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -