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

🚑 [ADD] KRX request시 header를 추가한다. #218

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions krx/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
class KrxMarcapListing:
def __init__(self, market):
self.market = market
self.headers = {
'User-Agent': 'Chrome/78.0.3904.87 Safari/537.36',
'Referer': 'http://data.krx.co.kr/'
}

def read(self):
url = 'http://data.krx.co.kr/comm/bldAttendant/executeForResourceBundle.cmd?baseName=krx.mdc.i18n.component&key=B128.bld'
j = json.loads(requests.get(url).text)
j = json.loads(requests.get(url, headers=self.headers).text)
date_str = j['result']['output'][0]['max_work_dt']

mkt_map = {'KRX-MARCAP':'ALL', 'KRX':'ALL', 'KOSPI':'STK', 'KOSDAQ':'KSQ', 'KONEX':'KNX'}
Expand All @@ -29,7 +33,7 @@ def read(self):
'money': '1',
'csvxls_isNo': 'false',
}
j = json.loads(requests.post(url, data).text)
j = json.loads(requests.post(url, headers=self.headers, data=data).text)
df = pd.DataFrame(j['OutBlock_1'])
df = df.replace(',', '', regex=True)
numeric_cols = ['CMPPREVDD_PRC', 'FLUC_RT', 'TDD_OPNPRC', 'TDD_HGPRC', 'TDD_LWPRC',
Expand All @@ -51,6 +55,10 @@ def read(self):
class KrxStockListing: # descriptive information
def __init__(self, market):
self.market = market
self.headers = {
'User-Agent': 'Chrome/78.0.3904.87 Safari/537.36',
'Referer': 'http://data.krx.co.kr/'
}

def read(self):
# KRX 상장회사목록
Expand All @@ -62,7 +70,7 @@ def read(self):
raise ValueError(f"market shoud be one of {mkt_list}")

url = 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13'
r = requests.get(url)
r = requests.get(url, headers=self.headers)
dfs = pd.read_html(io.StringIO(r.text), header=0)
df_listing = dfs[0]
cols_ren = {'회사명':'Name', '종목코드':'Code', '업종':'Sector', '주요제품':'Industry',
Expand All @@ -74,7 +82,7 @@ def read(self):

# KRX 주식종목검색
data = {'bld': 'dbms/comm/finder/finder_stkisu',}
r = requests.post('http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd', data=data)
r = requests.post('http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd',headers=self.headers, data=data)
jo = json.loads(r.text)
df_finder = pd.DataFrame(jo['block1'])

Expand Down Expand Up @@ -104,6 +112,7 @@ def read(self):
class KrxDelisting:
def __init__(self, market):
self.market = market
self.header = {'User-Agent': 'Chrome/78.0.3904.87 Safari/537.36'}

def read(self):
data = {
Expand All @@ -117,10 +126,8 @@ def read(self):
'csvxls_isNo': 'true',
}

headers = {'User-Agent': 'Chrome/78.0.3904.87 Safari/537.36',}

url = 'http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd'
r = requests.post(url, data, headers=headers)
r = requests.post(url, data, headers=self.headers)
j = json.loads(r.text)
df = pd.DataFrame(j['output'])
col_map = {'ISU_CD':'Symbol', 'ISU_NM':'Name', 'MKT_NM':'Market',
Expand Down