1. Revisit the meaning of coding (programming)

English translation

Contents

!pip install yfinance
Collecting yfinance
  Downloading yfinance-0.1.70-py2.py3-none-any.whl (26 kB)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance) (0.0.10)
Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.21.5)
Requirement already satisfied: pandas>=0.24.0 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.3.5)
Collecting lxml>=4.5.1
  Downloading lxml-4.8.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.4 MB)
     |████████████████████████████████| 6.4 MB 7.2 MB/s 
[?25hCollecting requests>=2.26
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 1.5 MB/s 
[?25hRequirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2018.9)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2.8.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2021.10.8)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.0.12)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (1.24.3)
Installing collected packages: requests, lxml, yfinance
  Attempting uninstall: requests
    Found existing installation: requests 2.23.0
    Uninstalling requests-2.23.0:
      Successfully uninstalled requests-2.23.0
  Attempting uninstall: lxml
    Found existing installation: lxml 4.2.6
    Uninstalling lxml-4.2.6:
      Successfully uninstalled lxml-4.2.6
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
google-colab 1.0.0 requires requests~=2.23.0, but you have requests 2.27.1 which is incompatible.
datascience 0.10.6 requires folium==0.2.1, but you have folium 0.8.3 which is incompatible.
Successfully installed lxml-4.8.0 requests-2.27.1 yfinance-0.1.70
Mounted at /content/drive

1.1 The main purpose of coding == 'Automation'

Automation

1.2 Automation example 1 - Automatically retrieve and plot KOSPI closing prices

from matplotlib import pyplot as plt
import yfinance as yf

# Daily KOSPI Adjusted Close. Internet access is required on the first run.
df = yf.download("^KS11", period="5y", auto_adjust=False,
                 multi_level_index=False, progress=False)
if df.empty:
    raise RuntimeError("KOSPI 데이터를 받지 못했습니다. 네트워크 연결을 확인한 뒤 다시 실행하세요.")
y_values = df['Adj Close']
x_values = range(len(y_values))

plt.plot(x_values, y_values)
plt.show()
[*********************100%***********************]  1 of 1 completed
<Figure size 432x288 with 1 Axes>
실행 결과

1.3 Automation example 2 - Automatically retrieve news headlines

import urllib.request
import xml.etree.ElementTree as ET

feed_url = 'https://www.yna.co.kr/rss/news.xml'
with urllib.request.urlopen(feed_url, timeout=15) as response:
    feed = ET.fromstring(response.read())

headlines = [item.findtext('title', default='').strip()
             for item in feed.findall('./channel/item')[:20]]
for title in headlines:
    print(title)