我如何将这个&符号视为文本?

时间:2017-08-18 13:27:24

标签: python google-finance-api

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 个答案:

答案 0 :(得分:0)

该库非常幼稚,因为它只是在没有正确编码的情况下将符号附加到URL,请参阅the source code

struct error
{
    int max_error;
    int applied_offset;
    int min_error;
    int sum_squares;
    std::vector<int> results;
};


TEST(CalculationsTest,PopulateError)
{
    Calculations c;
    std::vector<int> data = {1,2,3,4,5};
    QVector<error> e1 = c.PopulateErrorSet(data);
    QVector<error> e2= c.PopulateErrorSet(data);

    std::vector<error> e11 = e1.toStdVector();
    e2.begin()->max_error = 12;
    std::vector<error> e12 = e2.toStdVector();

    //EXPECT_THAT(1,0);
    ASSERT_THAT(e11, ::testing::ContainerEq(e12));
}

您可以使用urllib.parse.quote() function

手动引用前面的方法解决此问题
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

演示:

from urllib.parse import quote

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