-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_csv_stock.py
More file actions
28 lines (23 loc) · 952 Bytes
/
12_csv_stock.py
File metadata and controls
28 lines (23 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import csv
import requests
from bs4 import BeautifulSoup
url = "https://finance.naver.com/sise/sise_market_sum.nhn?sosok=0&page="\
filename = "시가총액1-200.csv"
f = open(filename, "w", encoding="utf-8-sig", newline="")
writer = csv.writer(f)
title = "N 종목명 현재가 전일비 등락률 액면가 시가총액 상장주식수 외국인비율 거래량 PER ROE".split("\t")
# ["N", "종목명", "현재가", ...]
# print(type(title))
writer.writerow(title)
for page in range(1, 5):
res = requests.get(url + str(page))
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")
data_rows = soup.find("table", attrs={"class":"type_2"}).find("tbody").find_all("tr")
for row in data_rows:
columns = row.find_all("td")
if len(columns) <= 1: # 의미 없는 데이터는 skip
continue
data = [column.get_text() for column in columns]
#print(data)
writer.writerow(data)