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

fix the bug that the HS_SYMBOLS_URL is 404 #1758

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions scripts/data_collector/pit/collector.py
Expand Up @@ -88,8 +88,12 @@ def get_instrument_list(self) -> List[str]:
return symbols

def normalize_symbol(self, symbol: str) -> str:
symbol, exchange = symbol.split(".")
exchange = "sh" if exchange == "ss" else "sz"
if symbol.startswith('6'):
exchange = 'sh'
elif symbol.startswith('0') or symbol.startswith('3'):
exchange = 'sz'
else:
exchange = 'bj'
return f"{exchange}{symbol}"

@staticmethod
Expand Down Expand Up @@ -201,8 +205,13 @@ def get_data(
) -> pd.DataFrame:
if interval != self.INTERVAL_QUARTERLY:
raise ValueError(f"cannot support {interval}")
symbol, exchange = symbol.split(".")
exchange = "sh" if exchange == "ss" else "sz"
if symbol.startswith('6'):
exchange = 'sh'
elif symbol.startswith('0') or symbol.startswith('3'):
exchange = 'sz'
else:
exchange = 'bj'

code = f"{exchange}.{symbol}"
start_date = start_datetime.strftime("%Y-%m-%d")
end_date = end_datetime.strftime("%Y-%m-%d")
Expand Down
29 changes: 18 additions & 11 deletions scripts/data_collector/utils.py
Expand Up @@ -190,17 +190,24 @@ def get_hs_stock_symbols() -> list:
global _HS_SYMBOLS # pylint: disable=W0603

def _get_symbol():
_res = set()
for _k, _v in (("ha", "ss"), ("sa", "sz"), ("gem", "sz")):
resp = requests.get(HS_SYMBOLS_URL.format(s_type=_k), timeout=None)
_res |= set(
map(
lambda x: "{}.{}".format(re.findall(r"\d+", x)[0], _v), # pylint: disable=W0640
etree.HTML(resp.text).xpath("//div[@class='result']/ul//li/a/text()"), # pylint: disable=I1101
)
)
time.sleep(3)
return _res
url = "http://99.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10000&po=1&np=1&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f12"
resp = requests.get(url, timeout=None)
if resp.status_code != 200:
raise ValueError("request error")

try:
_symbols = [_v["f12"] for _v in resp.json()["data"]["diff"]]
except Exception as e:
logger.warning(f"request error: {e}")
raise

if len(_symbols) < 3900:
raise ValueError("request error")

_symbols = [_symbol + '.ss' if _symbol.startswith('6') else _symbol + '.sz' if _symbol.startswith(('0', '3')) else None for _symbol in _symbols]
_symbols = [_symbol for _symbol in _symbols if _symbol is not None]

return set(_symbols)

if _HS_SYMBOLS is None:
symbols = set()
Expand Down