sharebook-kr/pykrx

상장종목 검색 Fetch 시 500 에러로 인해 일부 모듈 사용 불가

Closed this issue · 3 comments

에러 설명

class 상장종목검색(KrxWebIo):
@property
def bld(self):
return "dbms/comm/finder/finder_stkisu"
def fetch(self, mktsel: str = "ALL", searchText: str = "") -> DataFrame:
"""[12003] 개별종목 시세 추이에서 검색 버튼 눌러 활성화 되는 종목 검색창
스크래핑
Args:
mktsel (str, optional): 조회 시장 (STK/KSQ/ALL)
searchText (str, optional): 검색할 종목명 - 입력하지 않을 경우 전체
Returns:
DataFrame : 상장 종목 정보를 반환
full_code short_code codeName marketCode marketName
0 KR7060310000 060310 3S KSQ 코스닥
1 KR7095570008 095570 AJ네트웍스 STK 유가증권
2 KR7006840003 006840 AK홀딩스 STK 유가증권
3 KR7054620000 054620 APS홀딩스 KSQ 코스닥
4 KR7265520007 265520 AP시스템 KSQ 코스닥
marketEngName ord1 ord2
KOSDAQ 16
KOSPI 16
KOSPI 16
KOSDAQ 16
KOSDAQ 16
"""
result = self.read(locale="ko_KR", mktsel=mktsel,
searchText=searchText, typeNo=0)
return DataFrame(result['block1'])

위 코드에서 http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd 주소에 dbms/comm/finder/finder_stkisu bld 명령을 보냅니다

이렇게 요청을 보냈을때 서버에서 500 에러를 반환해서 에러가 나고 있는 상황입니다.

특히 주식을 조회하는 get_market_ohlcv 등의 함수에서 위 코드를 통해 먼저 주식 존재 여부를 확인하고 있어서, Side Effect가 있어 보이네요

에러 예시 코드

  1. pykrx 이용
from pykrx import stock

df = stock.get_market_ohlcv("20180101", "20181231", "001440")
  1. requests 이용
import requests
def get_stock_Indices():
    
    url = 'http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd'
    
    response = requests.post(url, data={
        'locale': 'ko_KR',
        'mktsel': 'ALL',
        'searchText': '',
        'typeNo': 0, 'bld':
        'dbms/comm/finder/finder_stkisu'
    })
    stock_data = response.json()['output']
    
    return stock_data

print(get_stock_Indices())

Error Traceback

Traceback (most recent call last):
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen      
    httplib_response = self._make_request(
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\OWNER\Documents\_github\_kw\2023_VIP\test2.py", line 3, in <module>
    df = stock.get_market_ohlcv("20180101", "20181231", "001440")
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\stock\stock_api.py", line 189, in get_market_ohlcv
    return get_market_ohlcv_by_date(*args, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\stock\stock_api.py", line 230, in get_market_ohlcv_by_date
    df = krx.get_market_ohlcv_by_date(fromdate, todate, ticker, adjusted)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\comm\util.py", line 8, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\market\wrap.py", line 50, in get_market_ohlcv_by_date
    isin = get_stock_ticker_isin(ticker)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\comm\util.py", line 8, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\market\ticker.py", line 62, in get_stock_ticker_isin
    s = StockTicker().get(ticker)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\comm\util.py", line 30, in __init__
    super(class_w, self).__init__(*args, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\market\ticker.py", line 12, in __init__
    self.listed = self.__fetch(상장종목검색)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\comm\util.py", line 8, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\market\ticker.py", line 19, in __fetch
    df = what().fetch(market)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\market\core.py", line 36, in fetch
    result = self.read(locale="ko_KR", mktsel=mktsel,
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\krx\krxio.py", line 56, in read
    resp = super().read(**params)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\pykrx\website\comm\webio.py", line 27, in read
    resp = requests.post(self.url, headers=self.headers, data=params)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\OWNER\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
mr-yoo commented

저 코드가 동작안하면 pykrx 모듈 전체가 동작안할거에요.

from pykrx.website.krx.market.core import 상장종목검색

s = 상장종목검색()
s.fetch()

위와 같이 직접 호출했을 때 정상 동작합니다.

확인해보니 저때 당시에만 일시적으로 안됐던 문제 같네요

이상한 점은 저때 공식 krx 사이트 들어가서 해당 백엔드 API 사용하는 공식 사이트를 조회했을땐 잘됐어서.. 뭔가 세션 단위 검사가 추가되었나 했습니다.

아무튼 추후에도 문제가 생기지 않으면 Close 해도 될것 같습니다. 확인해주셔서 감사합니다!

mr-yoo commented

요즘 KRX 서버의 변경이 있는지 불안정한 모습이 보이고 있습니다. report 감사합니다.