포트폴리오를 dataframe으로 구성하는 내용
-----------------------------------------------------------------------------------------------------------------------
import win32com.client
import pythoncom
import pandas as pd
import numpy as np
import time
import requests
import urllib3
from bs4 import BeautifulSoup
import json
import re
urllib3.disable_warnings()
# encparam 추출 함수 : naver증권에서 재무데이터 호출시 encparam이 필요하기 때문에 추출하는 함수
def get_encparam():
encparam=""
URL = "https://navercomp.wisereport.co.kr/v2/company/c1010001.aspx?cmp_cd=005930"
response_data = requests.get(URL, verify=False, headers={'referer': "https://finance.naver.com/item/coinfo.nhn?code=005930"})
html = response_data.text
regex = re.compile("encparam.*")
mo = regex.search(html)
if mo != None:
encparam = mo.group()[10:].replace('\'', '')
#print(mo.group()[10:].replace('\'',''))
return encparam
# 주식의 현재가 추출 함수
def get_currentprice(shcode):
urllib3.disable_warnings()
URL = "https://finance.naver.com/item/main.nhn?code=" + shcode
response_data = requests.get(URL, verify=False, headers={
'referer': "https://navercomp.wisereport.co.kr/v2/company/c1030001.aspx?cmp_cd=005930&cn="})
html = response_data.text
soup = BeautifulSoup(html, 'html.parser')
currentprice = int(soup.select('.trade_compare tr td')[0].contents[0].replace(',', ''))
return currentprice
# 주식의 종목명을 추출하는 함수
def get_hname(shcode):
urllib3.disable_warnings()
URL = "https://finance.naver.com/item/main.nhn?code=" + shcode
response_data = requests.get(URL, verify=False, headers={
'referer': "https://navercomp.wisereport.co.kr/v2/company/c1030001.aspx?cmp_cd=005930&cn="})
html = response_data.text
regex = re.compile("종목명 .*")
mo = regex.search(html)
hname = mo.group()[4:].replace('</dd>', '')
return hname
# 우선주인지 체크하여 우선주면 보통주의 종목코드로 변경하여 반환하는 함수 [우선주, 보통주]
def get_commonstock(shcode):
np_preference = np.array([["004365", "004360"], ["005945", "005940"], ["001045", "001040"], ["000815", "000810"], ["00088K", "000880"],
["078935", "078930"], ["005935", "005930"]])
df_preference = pd.DataFrame(data=np_preference, columns=['preference', 'mainstock'])
for i in range(len(df_preference.index)):
if df_preference.iloc[i,0] == shcode:
shcode = df_preference.iloc[i,1]
return shcode
# 1.potpolio dataframe 생성
np_potpolio_raw = np.array([["004960","",3,0,0,0,0,0], ["001940","",3,0,0,0,0,0], ["004970","",3,0,0,0,0,0], ["105560","",3,0,0,0,0,0], ["086790","",3,0,0,0,0,0],
["069510","",3,0,0,0,0,0], ["001500","",3,0,0,0,0,0], ["055550","",3,0,0,0,0,0], ["030610","",3,0,0,0,0,0], ["004365","",3,0,0,0,0,0],
["002920","",3,0,0,0,0,0], ["005490","",3,0,0,0,0,0], ["005945","",3,0,0,0,0,0], ["005810","",3,0,0,0,0,0], ["001045","",3,0,0,0,0,0],
["039490","",3,0,0,0,0,0], ["024110","",3,0,0,0,0,0], ["002460","",3,0,0,0,0,0], ["001270","",3,0,0,0,0,0], ["033780","",3,0,0,0,0,0],
["100250","",3,0,0,0,0,0], ["000815","",3,0,0,0,0,0], ["005960","",3,0,0,0,0,0], ["035000","",3,0,0,0,0,0], ["053210","",3,0,0,0,0,0],
["036190","",3,0,0,0,0,0], ["282690","",3,0,0,0,0,0], ["052330","",3,0,0,0,0,0], ["008060","",3,0,0,0,0,0], ["000480","",3,0,0,0,0,0],
["016360","",3,0,0,0,0,0], ["001720","",3,0,0,0,0,0], ["00088K","",3,0,0,0,0,0], ["267250","",3,0,0,0,0,0], ["078935","",3,0,0,0,0,0],
["010130","",3,0,0,0,0,0], ["100840","",3,0,0,0,0,0]
] )
df_potpolio = pd.DataFrame(data = np_potpolio_raw, columns=['shcode', 'hname', 'currentprice', 'buyprice','자산총계','부채총계','지배주주지분','순부채'])
# 2.현재가 및 종목명 update
print('update current price')
for i in range(len(df_potpolio.index)):
shcode = df_potpolio.iloc[i,0]
currentprice = get_currentprice(shcode)
hname = get_hname(shcode)
df_potpolio.iloc[i, 2] = currentprice
df_potpolio.iloc[i, 1] = hname
# 3.재무정보 update
for i in range(len(df_potpolio.index)):
#print(df_potpolio.iloc[i,0])
shcode = get_commonstock(df_potpolio.iloc[i,0])
#shcode = "105560"
print(shcode)
URL = "https://navercomp.wisereport.co.kr/v2/company/cF3002.aspx?cmp_cd="+ shcode + "&frq=0&rpt=1&finGubun=MAIN&frqTyp=0&cn=&encparam="+get_encparam()
response_data = requests.get(URL, verify=False, headers={'referer': "https://navercomp.wisereport.co.kr/v2/company/c1030001.aspx?cmp_cd=005930&cn="})
html = response_data.text
jsonObject = json.loads(html)
jsonArray = jsonObject.get("DATA")
if jsonArray is not None:
for j in range(len(jsonArray)):
if jsonArray[j].get("ACC_NM") in ("자산총계","자산"):
df_potpolio.iloc[i, 4] = jsonArray[j].get("DATAQ5")
elif jsonArray[j].get("ACC_NM") in ("부채총계", "부채"):
df_potpolio.iloc[i, 5] = jsonArray[j].get("DATAQ5")
elif jsonArray[j].get("ACC_NM") in ("지배주주지분", "....지배주주지분"):
df_potpolio.iloc[i, 6] = jsonArray[j].get("DATAQ5")
elif jsonArray[j].get("ACC_NM") == "*순부채":
df_potpolio.iloc[i, 7] = jsonArray[j].get("DATAQ5")
print(df_potpolio)
'Language > python' 카테고리의 다른 글
[ Python conda ] conda에서 github로부터 package install 하기 (0) | 2021.06.04 |
---|---|
[ Python library ] 특정 기업의 최근 5개 분기 대차대조표 Dataframe으로 반환 (0) | 2021.06.03 |
[ Python library ] 우선주를 보통주로 변환 함수 (0) | 2021.06.03 |
[ Python library ] 주식 종목명 추출 함수(naver증권 web crolling) (0) | 2021.06.03 |
[ Python library ] 주식 현재가 추출 함수(naver증권 webcrolling) (0) | 2021.06.03 |