Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在stock_get_quote_history中出现bug #23

Closed
TinkerJueBerg opened this issue Sep 27, 2021 · 13 comments
Closed

在stock_get_quote_history中出现bug #23

TinkerJueBerg opened this issue Sep 27, 2021 · 13 comments

Comments

@TinkerJueBerg
Copy link

原因大概是因为json的decoder有问题,
debug到decorder文件的这部分

def raw_decode(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3):
"""Decode a JSON document from s (a str or unicode
beginning with a JSON document) and return a 2-tuple of the Python
representation and the index in s where the document ended.
Optionally, idx can be used to specify an offset in s where
the JSON document begins.

    This can be used to decode a JSON document from a string that may
    have extraneous data at the end.

    """
    if idx < 0:
        # Ensure that raw_decode bails on negative indexes, the regex
        # would otherwise mask this behavior. #98
        raise JSONDecodeError('Expecting value', s, idx)
    if _PY3 and not isinstance(s, str):
        raise TypeError("Input string must be text, not bytes")
    # strip UTF-8 bom
    if len(s) > idx:
        ord0 = ord(s[idx])
        if ord0 == 0xfeff:
            idx += 1
        elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
            idx += 3
    return self.scan_once(s, idx=_w(s, idx).end())

报错是
JSONDecodeError: Expecting value

@Micro-sheep
Copy link
Owner

原因大概是因为json的decoder有问题,

debug到decorder文件的这部分

def raw_decode(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3):

    """Decode a JSON document from ``s`` (a ``str`` or ``unicode``

    beginning with a JSON document) and return a 2-tuple of the Python

    representation and the index in ``s`` where the document ended.

    Optionally, ``idx`` can be used to specify an offset in ``s`` where

    the JSON document begins.



    This can be used to decode a JSON document from a string that may

    have extraneous data at the end.



    """

    if idx < 0:

        # Ensure that raw_decode bails on negative indexes, the regex

        # would otherwise mask this behavior. #98

        raise JSONDecodeError('Expecting value', s, idx)

    if _PY3 and not isinstance(s, str):

        raise TypeError("Input string must be text, not bytes")

    # strip UTF-8 bom

    if len(s) > idx:

        ord0 = ord(s[idx])

        if ord0 == 0xfeff:

            idx += 1

        elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':

            idx += 3

    return self.scan_once(s, idx=_w(s, idx).end())

报错是

JSONDecodeError: Expecting value

把核心调用代码发一下,我本地试试看,还有提供一下 python 版本、操作系统以及 efinance 的版本号。

@TinkerJueBerg
Copy link
Author

原因大概是因为json的decoder有问题,
debug到decorder文件的这部分
def raw_decode(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3):

    """Decode a JSON document from ``s`` (a ``str`` or ``unicode``

    beginning with a JSON document) and return a 2-tuple of the Python

    representation and the index in ``s`` where the document ended.

    Optionally, ``idx`` can be used to specify an offset in ``s`` where

    the JSON document begins.



    This can be used to decode a JSON document from a string that may

    have extraneous data at the end.



    """

    if idx < 0:

        # Ensure that raw_decode bails on negative indexes, the regex

        # would otherwise mask this behavior. #98

        raise JSONDecodeError('Expecting value', s, idx)

    if _PY3 and not isinstance(s, str):

        raise TypeError("Input string must be text, not bytes")

    # strip UTF-8 bom

    if len(s) > idx:

        ord0 = ord(s[idx])

        if ord0 == 0xfeff:

            idx += 1

        elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':

            idx += 3

    return self.scan_once(s, idx=_w(s, idx).end())

报错是
JSONDecodeError: Expecting value

把核心调用代码发一下,我本地试试看,还有提供一下 python 版本、操作系统以及 efinance 的版本号。

stock_codes= '招商积余' # 001914也一样
df = ef.stock.get_quote_history(stock_codes=code,klt=1)
这步调用就直接报错了,奇怪的是它是间歇性的,一段时间内怎么运行都bug。过了一会运行就可能没错。
py3.8 ef0.3.8 mac spyder和pycharm都有这个问题。
麻烦大佬debug了哈

@Micro-sheep
Copy link
Owner

我在 Windows 系统的 python 3.7 版本下跑了 100 次,均是正常的,在 docker 里面的 miniconda 管理的 python 3.7 版本也是正常。测试代码如下

import efinance as ef
# 对同一只股票测试 100 次
ef.stock.get_quote_history(['招商积余']*100) 

@TinkerJueBerg
Copy link
Author

你试一下等几分钟再跑一次,这个偶尔会通,似乎和时间段有关,原因应该是获取请求但是没有返回数据。

@Micro-sheep
Copy link
Owner

你试一下等几分钟再跑一次,这个偶尔会通,似乎和时间段有关,原因应该是获取请求但是没有返回数据。

klt=1 的话,如果是交易日,得在非早上 9点到9点半之间运行。因为这个时间段东方财富会重置分钟线数据。

@TinkerJueBerg
Copy link
Author

现在也不能用,凡是交易时段就不用能是吧,,

@Micro-sheep
Copy link
Owner

刚刚也试了,没啥问题的
image
如果你的还是不行,可以试试重新安装一遍 efinance

@Micro-sheep
Copy link
Owner

原因大概是因为json的decoder有问题,
debug到decorder文件的这部分
def raw_decode(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3):

    """Decode a JSON document from ``s`` (a ``str`` or ``unicode``

    beginning with a JSON document) and return a 2-tuple of the Python

    representation and the index in ``s`` where the document ended.

    Optionally, ``idx`` can be used to specify an offset in ``s`` where

    the JSON document begins.



    This can be used to decode a JSON document from a string that may

    have extraneous data at the end.



    """

    if idx < 0:

        # Ensure that raw_decode bails on negative indexes, the regex

        # would otherwise mask this behavior. #98

        raise JSONDecodeError('Expecting value', s, idx)

    if _PY3 and not isinstance(s, str):

        raise TypeError("Input string must be text, not bytes")

    # strip UTF-8 bom

    if len(s) > idx:

        ord0 = ord(s[idx])

        if ord0 == 0xfeff:

            idx += 1

        elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':

            idx += 3

    return self.scan_once(s, idx=_w(s, idx).end())

报错是
JSONDecodeError: Expecting value

把核心调用代码发一下,我本地试试看,还有提供一下 python 版本、操作系统以及 efinance 的版本号。

stock_codes= '招商积余' # 001914也一样
df = ef.stock.get_quote_history(stock_codes=code,klt=1)
这步调用就直接报错了,奇怪的是它是间歇性的,一段时间内怎么运行都bug。过了一会运行就可能没错。
py3.8 ef0.3.8 mac spyder和pycharm都有这个问题。
麻烦大佬debug了哈

提供一下属于 efinance 的报错代码。原生的这些库基本上不会有问题,我得知道是 efinance 里面的哪一部分代码报错。

@TinkerJueBerg
Copy link
Author

Traceback (most recent call last):

File "/Users/ttt/.spyder-py3/temp.py", line 47, in
query()

File "/Users/ttt/.spyder-py3/temp.py", line 29, in query
df = ef.stock.get_quote_history(stock_codes=code,klt=1)

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/efinance/stock/getter.py", line 235, in get_quote_history
df = get_quote_history_for_stock(

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/efinance/common/getter.py", line 190, in get_quote_history
return get_quote_history_single(codes,

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/efinance/utils/init.py", line 33, in run
values = func(*args, **kwargs)

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/efinance/common/getter.py", line 87, in get_quote_history_single
json_response = session.get(

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/simplejson/init.py", line 525, in loads
return _default_decoder.decode(s)

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)

File "/Users/ttt/opt/anaconda3/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())

JSONDecodeError: Expecting value

@TinkerJueBerg
Copy link
Author

那我理解就是run 这个函数没有读到值

@Micro-sheep
Copy link
Owner

那我理解就是run 这个函数没有读到值

你试一下别的股票代码,然后klt 保持默认,如果可以,那就重新安装 efinace 再试之前报错的代码,避免搜索缓存更新有问题。如果还是不行,我再远程帮你看看吧,我这边没法复现问题。

@TinkerJueBerg
Copy link
Author

我知道了,你的url请求的这个域名和api返回经常是502就是报错,用浏览器和requests请求都试过了。
'http://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get'。'http://push2his.eastmoney.com/

@Micro-sheep
Copy link
Owner

Micro-sheep commented Sep 28, 2021

我知道了,你的url请求的这个域名和api返回经常是502就是报错,用浏览器和requests请求都试过了。 'http://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get'。'http://push2his.eastmoney.com/

嗯,这估计跟网络有关吧。国内网络直连一般是正常的,至少我的没有出现过问题,无论使用的是个人电脑、服务器、手机热点的网络。你检查一下是不是用了代理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants