Last active
August 16, 2017 00:17
-
-
Save hyunsikhwang/386dddc61a8aecae4afde8d42e72587a to your computer and use it in GitHub Desktop.
KRX 시장 통계 데이터를 가져오는 방법입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import bs4 | |
import xml.etree | |
import datetime | |
def post_beautiful_soup(url): | |
return bs4.BeautifulSoup(requests.post(url, data=payload).text, "html5lib") | |
def get_beautiful_soup(url): | |
return bs4.BeautifulSoup(requests.get(url).text, "html5lib") | |
def millis(): | |
return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) | |
milli_timestamp = millis() | |
#print(milli_timestamp) | |
url_1 = 'http://marketdata.krx.co.kr/contents/COM/GenerateOTP.jspx?bld=MKD%2F10%2F1001%2F10010101%2Fmkd10010101_07&name=selectbox&_=' + str(milli_timestamp) | |
OTP = get_beautiful_soup(url_1).find('body').text | |
print(OTP) | |
#url_2 = 'http://marketdata.krx.co.kr/contents/MKD/99/MKD99000001.jspx?type=1&ind_type=1001&period_strt_dd=20170804&period_end_dd=20170811&pagePath=%2Fcontents%2FMKD%2F10%2F1001%2F10010101%2FMKD10010101.jsp&code=' + OTP + '&pageFirstCall=Y' | |
url_2 = 'http://marketdata.krx.co.kr/contents/MKD/99/MKD99000001.jspx' | |
payload = {'type':'1', 'ind_type':'1001', 'period_strt_dd':'20170804', 'period_end_dd':'20170811', 'pagePath':'pagePath:/contents/MKD/10/1001/10010101/MKD10010101.jsp','code':OTP, 'pageFirstCall':'Y'} | |
headers = {'Content-Type': 'application/xml'} | |
#MktData = get_beautiful_soup(url_2) | |
MktData = post_beautiful_soup(url_2) | |
print(MktData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment