Skip to content

Commit c4d36c0

Browse files
committed
Parse XML data
1 parent 64f1faf commit c4d36c0

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

pandas_datareader/naver.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from xml.etree import ElementTree
2+
3+
from pandas import DataFrame
14
from pandas_datareader.base import _DailyBaseReader
25

36
class NaverDailyReader(_DailyBaseReader):
@@ -51,6 +54,31 @@ def _get_params(self, symbol):
5154
return params
5255

5356
def _read_one_data(self, url, params):
54-
"""Read one data from specified symbol"""
57+
"""Read one data from specified symbol.
58+
59+
:rtype: DataFrame
60+
"""
5561
resp = self._get_response(url, params=params)
56-
return resp
62+
parsed = self._parse_xml_response(resp.text)
63+
prices = DataFrame(parsed, columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])
64+
65+
return prices
66+
67+
def _parse_xml_response(self, xml_content):
68+
"""Parses XML response from the server.
69+
70+
An example of response:
71+
72+
<?xml version="1.0" encoding="EUC-KR" ?>
73+
<protocol>
74+
<chartdata symbol="005930" name="삼성전자" count="500" timeframe="day" precision="0" origintime="19900103">
75+
<item data="20170918|218500|222000|217000|220500|72124" />
76+
<item data="20170919|218000|221000|217500|219000|62753" />
77+
...
78+
</protocol>
79+
"""
80+
root = ElementTree.fromstring(xml_content)
81+
items = root.findall("chartdata/item")
82+
83+
for item in items:
84+
yield item.attrib["data"].split("|")

0 commit comments

Comments
 (0)