티스토리 뷰

반응형
1. 네이버 주식 시가총액 목록 가져오기 (https://finance.naver.com/sise/sise_market_sum.nhn)


2. 기업 별 상세 재무 데이터 가져오기 (https://finance.naver.com/item/main.nhn?code=기업코드)
  - def getDataOfParam(param)

3. Python Dictionary 사용하여 데이터 정리


4. 원하는 데이터 지표 추출 (매출액, 영업이익, 당기순이익, ROE, PER, PBR)
  - def printRecommendedItems(stock):


5. 추출 데이터 메일로 자동 전송하기
  - def sendEmailfunc(text):

의 5단계를 통해서 네이버 주식정보 가져오는 방법을 포스팅 하려고 한다. 

 

이번 포스팅에서는 3, 4번에 해당하는 내용이다. 

 

앞선 포스팅(cocoabba.tistory.com/21)에서와 같이 원하는 데이터를 가져온 이후에 어떻게 데이터를 저장하고,

원하는 조건에 맞는 데이터를 추출할지 내용을 공유하려고 한다.

 

[PYTHON] 네이버 주식정보 가져오기 (2/4)

1. 네이버 주식 시가총액 목록 가져오기 (https://finance.naver.com/sise/sise_market_sum.nhn) 2. 기업 별 상세 재무 데이터 가져오기 (https://finance.naver.com/item/main.nhn?code=기업코드) 3. Python Dict..

cocoabba.tistory.com

앞선 포스팅까지의 과정을 잘 진행하면 아래와 같은 데이터를 얻을 수 있다. 

['2017.12', '2018.12', '2019.12', '2019.09', '2019.12', '2020.12(E)', '2020.03', '2020.06', '2020.09', '2020.12(E)']
매출액  :  ['2,395,754', '2,437,714', '2,304,009', '2,381,935', '620,035', '598,848', '553,252', '529,661', '669,642', '629,382']
영업이익  :  ['536,450', '588,867', '277,685', '370,138', '77,779', '71,603', '64,473', '81,463', '123,532', '100,683']
당기순이익  :  ['421,867', '443,449', '217,389', '278,073', '62,877', '52,270', '48,849', '55,551', '93,607', '78,142']
ROE(지배주주)  :  ['21.01', '19.63', '8.69', '10.42', '10.05', '8.69', '8.45', '8.49', '9.51', '']
PER()  :  ['9.40', '6.42', '17.63', '18.09', '13.73', '17.63', '15.24', '16.52', '15.89', '67.69']
PBR()  :  ['1.67', '1.09', '1.49', '1.82', '1.30', '1.49', '1.25', '1.37', '1.48', '1.82']

#dictionary 데이터 정리

{
  'name': '
삼성전자',
  'idx': 0,
  'link': 'https://finance.naver.com//item/main.nhn?code=005930', 
  'curCost': '73,400',
  '
매출액': ['2,395,754', '2,437,714', '2,304,009', '2,381,935', '620,035', '598,848', '553,252', '529,661', '669,642', '629,382'],
  '
영업이익': ['536,450', '588,867', '277,685', '370,138', '77,779', '71,603', '64,473', '81,463', '123,532', '100,683'],
  '
당기순이익': ['421,867', '443,449', '217,389', '278,073', '62,877', '52,270', '48,849', '55,551', '93,607', '78,142'],
  'ROE(
지배주주)': ['21.01', '19.63', '8.69', '10.42', '10.05', '8.69', '8.45', '8.49', '9.51', ''],
  'PER(
)': ['9.40', '6.42', '17.63', '18.09', '13.73', '17.63', '15.24', '16.52', '15.89', '67.69'],
  'PBR(
)': ['1.67', '1.09', '1.49', '1.82', '1.30', '1.49', '1.25', '1.37', '1.48', '1.82'],
  'img_month3': 'https://ssl.pstatic.net/imgfinance/chart/item/area/month3/005930.png?sidcode=1607865392294', 
  'img_year': 'https://ssl.pstatic.net/imgfinance/chart/item/area/year/005930.png?sidcode=1607865392294', 
  'img_year3': 'https://ssl.pstatic.net/imgfinance/chart/item/area/year3/005930.png?sidcode=1607865392294'
}

위의 데이터에서 설명하지 않은 부분은 img_month3 이미지를 추출하는 부분을 설명하지 않았다.

하지만, 이 부분은 앞선 포스팅을 통해 충분히 할 수 있는 부분이라고 생각해서 SKIP 하려고 한다. 

(혹시 img_month3/year/year3 경로를 추출하기 어려우신 분은 댓글을 남겨주시면 별도 포스팅으로 설명드리겠음^^)

 

본 포스팅에서는 이렇게 생생된 dictionary 데이터 중 원하는 기준에 적합한지 여부와 html 형태로 변환까지 진행하려고 한다. 

 

아래 코드는 printRecommendedItems(stock) 함수 Code 이다. 

 

    for idx in range(4, 9):
        if stock['ROE(지배주주)'][idx] is None or not stock['ROE(지배주주)'][idx] or\
            stock['PER(배)'][idx] is None or not stock['PER(배)'][idx] or\
            stock['PBR(배)'][idx] is None or not stock['PBR(배)'][idx] :
            # print(idx)
            continue
        if float(stock['ROE(지배주주)'][idx].replace(",","")) > ROE_BASE:
            numberROE += 1
        if float(stock['PER(배)'][idx].replace(",","")) < PER_BASE:
            numberPER += 1
        if float(stock['PBR(배)'][idx].replace(",","")) < PBR_BASE:
            numberPBR += 1

        if numberROE >= 2 and numberPER >= 2 and numberPBR >= 2:


            resultString += "<a href = '"+stock['link']+"'> "+str(stock['idx'])+". "+stock['name']+"</a> - 현재가 : "+stock['curCost']
            resultString += "<table border='1px'><tr><td>"+"<td>".join(dataHeader)+"</tr>"
            resultString += "<br><tr><td>매출액<td>"+"<td>".join(stock['매출액'])+"</tr>"
            resultString += "<br><tr><td>당기순이익<td>" + "<td>".join(stock['당기순이익'])+"</tr>"
            resultString += "<br><tr><td>ROE<td>" + "<td>".join(stock['ROE(지배주주)'])+"</tr>"
            resultString += "<br><tr><td>PER<td>" + "<td>".join(stock['PER(배)'])+"</tr>"
            resultString += "<br><tr><td>PBR<td>" + "<td>".join(stock['PBR(배)'])+"</tr></table>"
            resultString += "<br><br><br>[3개월]<br><img src='" + stock['img_month3']+"'>"
            resultString += "<br><br>[1년]<br><img src='" + stock['img_year']+"'>"
            resultString += "<br><br>[3년]<br><img src='" + stock['img_year3']+"'>"

            # print(resultString)
            return 1, resultString

        else:
            continue

for loop 를 통해 인덱스가 최근 6개 분기간 내가 원하는 기준을 달성했는지를 체크하는 로직이다. 

(참고로 주식 입문자인 저는 1. PER 30% 미만 2. PBR 5 미만 3. ROE 10 이상으로 함)

 

그리고, 세개의 지표 모두 2번 이상 달성했을 경우, 해당 stock 의 데이터를 출력하도록 하였다. 

 

결과로 출력할 변수인 resultString 값을 HTML 로 작성한 이유는 본 텍스트를 메일로 전송하여, 
링크 및 이미지를 메일본문 내에서 표현하기 위해서 HTML 로 작성하였다. 

 


마지막 포스팅으로 메일 전달과정을 공유하겠다.

 

[PYTHON] 네이버 주식정보 가져오기 (4/4) - email 보내기

https://cocoabba.tistory.com/23?category=439259

 

[PYTHON] 네이버 주식정보 가져오기 (4/4) - email 보내기

1. 네이버 주식 시가총액 목록 가져오기 (https://finance.naver.com/sise/sise_market_sum.nhn) 2. 기업 별 상세 재무 데이터 가져오기 (https://finance.naver.com/item/main.nhn?code=기업코드) - def getDat..

cocoabba.tistory.com

 

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함