코드:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import bs4
import urllib.request
import openpyxl
from datetime import date
import string
 
today = date.today()
= []
= []
= []
def get_company():
    url = "https://finance.naver.com/sise/sise_rise.nhn?sosok=1"
    html = urllib.request.urlopen(url)
    bsobj = bs4.BeautifulSoup(html, "html.parser")
    tltle = bsobj.find_all("a", {"class""tltle"})
    for i in tltle:
        a.append(i.text)
    return
 
def get_price():
    url = "https://finance.naver.com/sise/sise_rise.nhn?sosok=1"
    html = urllib.request.urlopen(url)
    bsobj = bs4.BeautifulSoup(html, "html.parser")
    price = bsobj.find_all("td", {"class""number"})
    for i in price:
        b.append(i.text)
    for index, value in enumerate(b):
        if index%10 == 0:
            c.append(value.replace(","""))
    return
 
 
def upload_every_morning():
    get_company()
    get_price()
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.append(["company""price"])
    for i in range(0320):
        sheet.append([a[i], c[i]])
    wb.save("E:\\3_2\\python\\Tkinter\\stock_data\\" + str(today) + "_morning.xlsx")
    return
 
def upload_every_final():
    get_company()
    get_price()
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.append(["company""price"])
    for i in range(0320):
        sheet.append([a[i], c[i]])
    wb.save("E:\\3_2\\python\\Tkinter\\stock_data\\" + str(today) + "_final.xlsx")
    return
 
upload_every_morning()
upload_every_final()
 
cs

 

실행결과:

 

 

설명:

today = date.today()

today()함수를 이용하여 오늘 날짜를 today변수에 저장

 

get_company()

get_price()

회사명과 주가를 크롤링. 자세한 설명은 아래 포스팅 참고

Python,crawling, bs4,pandas,주식 엑셀로 가져오기

 

Python,crawling, bs4,pandas,주식 엑셀로 가져오기

한국 거래소에 올라와 있는 엑셀파일을 다운받는 것을 구현해보았다. 코드: 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 29 30 31 32 33 34 35 36 37 38 39 import requests imp..

coding-0830.tistory.com

wb = openpyxl.Workbook()

openpyxl.Workbook()함수를 이용하여 엑셀 작업 시작 (엑셀을 만드는 작업)

 

sheet = wb.active

뭔말인지 모름

 

sheet.append(["company""price"])

append()함수를 이용하여 엑셀 파일에서 이미 입력이 되어있는 셀 바로 밑 줄에 내용 추가, 현재는 빈 상태이기 때문에 가장 윗줄에 입력이 됨.

 

for i in range(0320):

      sheet.append([a[i], c[i]])

회사명이 저장되어있는 a배열과 주가가 저장되어있는 c배열의 내용을 입력.

 

wb.save("E:\\3_2\\python\\Tkinter\\stock_data\\" + str(today) + "_final.xlsx")

save()함수를 이용하여 해당 주소에 파일 저장.

 

 

+ Recent posts