from datetime import date, timedelta
import requests
class Krx():
def __init__(self, parent=None):
super().__init__()
@property
def url(self):
return "http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd"
@property
def headers(self):
return {
"User-Agent": "Mozilla/5.0 (Windows; Windows NT 6.1; rv:2.0b2) Gecko/20100720 Firefox/4.0b2",
"X-Requested-With": "XMLHttpRequest",
"Accept": "text/html",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
}
def 기간별_개별종목_공매도_종합정보(self, strtDd=None, endDd=None):
today = date.today()
if strtDd == None:
strtDd = (today - timedelta(7)).strftime("%Y%m%d")
if endDd == None:
endDd = today.strftime("%Y%m%d")
isuCd = 'KR7005930003' # 삼성정자
bld = 'dbms/MDC/STAT/srt/MDCSTAT30001'
url = self.url + '?bld=' + bld + '&isuCd=' + isuCd + '&strtDd=' + strtDd + '&endDd=' + endDd
page = requests.get(url, headers=self.headers)
try:
for r in page.json()['OutBlock_1']:
"""
TRD_DD': 'YYYY/mm/dd'
CVSRTSELL_TRDVOL: [전체 공매도 거래량] = 업틱룰 적용 공매도 거래량 + 업틱룰 예외 공매도 거래량
UPTICKRULE_APPL_TRDVOL: [업틱룰 적용 공매도 거래량]
UPTICKRULE_EXCPT_TRDVOL: [업틱룰 예외 공매도 거래량]
STR_CONST_VAL1: [남은 공매도 물량]
CVSRTSELL_TRDVAL: [전체 공매도 거래 금액]
UPTICKRULE_APPL_TRDVAL: [업틱룰 적용 공매도 거래 금액]
UPTICKRULE_EXCPT_TRDVAL: [업틱룰 예외 공매도 거래 금액]
STR_CONST_VAL2': [남은 공매도 금액]
"""
ymd = r['TRD_DD'].replace('/', '-')
s_vol = int(r['CVSRTSELL_TRDVOL'].replace(',', '').replace('-', ''))
s_amt = r['CVSRTSELL_TRDVAL'].replace(',', '').replace('-', '') # [전체 공매도 금액]
s_remain_vol = r['STR_CONST_VAL1'].replace(',', '').replace('-', '') # [남은 공매도 량]
s_remain_amt = r['STR_CONST_VAL2'].replace(',', '').replace('-', '') # [남은 공매도 금액]
if s_vol == '':
s_vol = 0
if s_amt == '':
s_amt = 0
if s_remain_vol == '':
s_remain_vol = 0
if s_remain_amt == '':
s_remain_amt = 0
# 해당일 해당종목에 대한 거래량을 가져와서 거래비율을 계산한다.
print(isuCd, ymd, s_vol, s_amt, s_remain_vol, s_remain_amt)
except Exception as e:
print('I got a Exception - reason "%s"' % str(e))
if __name__ == "__main__":
krx = Krx()
krx.기간별_개별종목_공매도_종합정보()
KR7005930003 2023-04-06 505314 31866842200 0 0
KR7005930003 2023-04-05 477136 30390382500 0 0
KR7005930003 2023-04-04 286562 18186340900 8832849 561769196400
KR7005930003 2023-04-03 534115 33875960300 8880931 560386746100
KR7005930003 2023-03-31 409099 26136851100 7964770 509745280000