KT AIVLE/Daily Review
241002
bestone888
2024. 10. 3. 19:25
241002
클래스
-
- 클래스 선언
-
- 객체 생성
-
- 메서드 호출
In [12]:
# 1. 클래스 선언
class Account:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
In [18]:
# 2. 객체 생성
acc1 = Account(1000) # 잔고 1000
acc2 = Account(3000) # 잔고 3000
In [21]:
# dir(): 객체에 들어있는 변수, 메서드 목록 출력
print(dir(acc1)[-3:])
['balance', 'deposit', 'withdraw']
In [23]:
# 3. 메서드 호출
acc1.deposit(1000)
acc2.withdraw(2000)
print(acc1.balance, acc2.balance)
2000 1000
In [ ]:
In [34]:
# 1. 클래스 선언
class Marine:
def __init__(self, health, ap):
self.health = health
self.ap = ap
def attack(self, unit):
unit.health -= self.ap
In [36]:
# 2. 객체 생성
m1 = Marine(40, 5) # 체력 40, 공격력 5
m2 = Marine(50, 6) # 체력 50, 공격력 6
In [38]:
# 3. 메서드 호출
In [40]:
m1.attack(m2)
m2.attack(m1)
print(m1.health, m2.health)
34 45
In [ ]:
웹크롤링 (Web Crawling)
-
- 웹서비스 분석: url = ???
-
- 서버에 데이터 요청: request.get(url)
-
- 서버에서 받은 데이터 형태 변경 json()
In [58]:
import requests
import pandas as pd
In [60]:
# 1. 웹서비스 분석
url = 'https://m.stock.naver.com/api/index/KOSPI/price?pageSize=20&page=1'
In [64]:
# 2. 서버에 데이터 요청
# status code가 200이 나오면 정상
# 403 or 500이면 request가 잘못되거나 web server에서 수집 안되도록 설정한 것
response = requests.get(url)
response
Out[64]:
<Response [200]>
In [66]:
# 3. 서버에서 받은 데이터 형태 변경 json() -> list, dict -> DataFrame
type(response.json())
Out[66]:
list
In [78]:
data = response.json()
data
Out[78]:
[{'localTradedAt': '2024-10-02',
'closePrice': '2,561.69',
'compareToPreviousClosePrice': '-31.58',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-1.22',
'openPrice': '2,566.55',
'highPrice': '2,591.61',
'lowPrice': '2,555.46'},
{'localTradedAt': '2024-09-30',
'closePrice': '2,593.27',
'compareToPreviousClosePrice': '-56.51',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-2.13',
'openPrice': '2,665.24',
'highPrice': '2,668.66',
'lowPrice': '2,593.27'},
{'localTradedAt': '2024-09-27',
'closePrice': '2,649.78',
'compareToPreviousClosePrice': '-21.79',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.82',
'openPrice': '2,674.58',
'highPrice': '2,681.45',
'lowPrice': '2,649.78'},
{'localTradedAt': '2024-09-26',
'closePrice': '2,671.57',
'compareToPreviousClosePrice': '75.25',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '2.90',
'openPrice': '2,630.91',
'highPrice': '2,671.57',
'lowPrice': '2,630.30'},
{'localTradedAt': '2024-09-25',
'closePrice': '2,596.32',
'compareToPreviousClosePrice': '-35.36',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-1.34',
'openPrice': '2,652.71',
'highPrice': '2,663.36',
'lowPrice': '2,596.32'},
{'localTradedAt': '2024-09-24',
'closePrice': '2,631.68',
'compareToPreviousClosePrice': '29.67',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '1.14',
'openPrice': '2,612.45',
'highPrice': '2,631.68',
'lowPrice': '2,597.81'},
{'localTradedAt': '2024-09-23',
'closePrice': '2,602.01',
'compareToPreviousClosePrice': '8.64',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.33',
'openPrice': '2,596.47',
'highPrice': '2,603.57',
'lowPrice': '2,588.49'},
{'localTradedAt': '2024-09-20',
'closePrice': '2,593.37',
'compareToPreviousClosePrice': '12.57',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.49',
'openPrice': '2,603.83',
'highPrice': '2,619.55',
'lowPrice': '2,591.40'},
{'localTradedAt': '2024-09-19',
'closePrice': '2,580.80',
'compareToPreviousClosePrice': '5.39',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.21',
'openPrice': '2,594.67',
'highPrice': '2,598.68',
'lowPrice': '2,550.09'},
{'localTradedAt': '2024-09-13',
'closePrice': '2,575.41',
'compareToPreviousClosePrice': '3.32',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.13',
'openPrice': '2,571.81',
'highPrice': '2,584.11',
'lowPrice': '2,562.91'},
{'localTradedAt': '2024-09-12',
'closePrice': '2,572.09',
'compareToPreviousClosePrice': '58.72',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '2.34',
'openPrice': '2,547.50',
'highPrice': '2,572.09',
'lowPrice': '2,537.87'},
{'localTradedAt': '2024-09-11',
'closePrice': '2,513.37',
'compareToPreviousClosePrice': '-10.06',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.40',
'openPrice': '2,524.86',
'highPrice': '2,526.13',
'lowPrice': '2,493.37'},
{'localTradedAt': '2024-09-10',
'closePrice': '2,523.43',
'compareToPreviousClosePrice': '-12.50',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.49',
'openPrice': '2,542.69',
'highPrice': '2,544.83',
'lowPrice': '2,522.48'},
{'localTradedAt': '2024-09-09',
'closePrice': '2,535.93',
'compareToPreviousClosePrice': '-8.35',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.33',
'openPrice': '2,498.67',
'highPrice': '2,543.22',
'lowPrice': '2,491.30'},
{'localTradedAt': '2024-09-06',
'closePrice': '2,544.28',
'compareToPreviousClosePrice': '-31.22',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-1.21',
'openPrice': '2,576.66',
'highPrice': '2,576.94',
'lowPrice': '2,529.31'},
{'localTradedAt': '2024-09-05',
'closePrice': '2,575.50',
'compareToPreviousClosePrice': '-5.30',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.21',
'openPrice': '2,598.36',
'highPrice': '2,615.80',
'lowPrice': '2,560.65'},
{'localTradedAt': '2024-09-04',
'closePrice': '2,580.80',
'compareToPreviousClosePrice': '-83.83',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-3.15',
'openPrice': '2,589.94',
'highPrice': '2,608.13',
'lowPrice': '2,578.07'},
{'localTradedAt': '2024-09-03',
'closePrice': '2,664.63',
'compareToPreviousClosePrice': '-16.37',
'compareToPreviousPrice': {'code': '5', 'text': '하락', 'name': 'FALLING'},
'fluctuationsRatio': '-0.61',
'openPrice': '2,683.12',
'highPrice': '2,695.59',
'lowPrice': '2,664.63'},
{'localTradedAt': '2024-09-02',
'closePrice': '2,681.00',
'compareToPreviousClosePrice': '6.69',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.25',
'openPrice': '2,683.80',
'highPrice': '2,686.98',
'lowPrice': '2,658.31'},
{'localTradedAt': '2024-08-30',
'closePrice': '2,674.31',
'compareToPreviousClosePrice': '12.03',
'compareToPreviousPrice': {'code': '2', 'text': '상승', 'name': 'RISING'},
'fluctuationsRatio': '0.45',
'openPrice': '2,676.72',
'highPrice': '2,686.51',
'lowPrice': '2,668.66'}]
In [80]:
df = pd.DataFrame(data)
df.head()
Out[80]:
localTradedAtclosePricecompareToPreviousClosePricecompareToPreviousPricefluctuationsRatioopenPricehighPricelowPrice01234
2024-10-02 | 2,561.69 | -31.58 | {'code': '5', 'text': '하락', 'name': 'FALLING'} | -1.22 | 2,566.55 | 2,591.61 | 2,555.46 |
2024-09-30 | 2,593.27 | -56.51 | {'code': '5', 'text': '하락', 'name': 'FALLING'} | -2.13 | 2,665.24 | 2,668.66 | 2,593.27 |
2024-09-27 | 2,649.78 | -21.79 | {'code': '5', 'text': '하락', 'name': 'FALLING'} | -0.82 | 2,674.58 | 2,681.45 | 2,649.78 |
2024-09-26 | 2,671.57 | 75.25 | {'code': '2', 'text': '상승', 'name': 'RISING'} | 2.90 | 2,630.91 | 2,671.57 | 2,630.30 |
2024-09-25 | 2,596.32 | -35.36 | {'code': '5', 'text': '하락', 'name': 'FALLING'} | -1.34 | 2,652.71 | 2,663.36 | 2,596.32 |
In [86]:
df = df.loc[:, ['localTradedAt', 'closePrice']]
df.tail()
Out[86]:
localTradedAtclosePrice1516171819
2024-09-05 | 2,575.50 |
2024-09-04 | 2,580.80 |
2024-09-03 | 2,664.63 |
2024-09-02 | 2,681.00 |
2024-08-30 | 2,674.31 |