한경 매수 리포트 수집
## 클래스 구성 요약
- ReportFetcher
목적: 한경 컨센서스에서 당일 리포트 크롤링 및 매수 리포트 필터링
핵심 메서드:
fetch(): HTML 파싱 후 테이블 추출 → DataFrame 반환
filter_buy_reports(df): 매수 의견 포함 리포트 필터링 + 기업명/종목코드 추출
- NewsSearcher
목적: 필터링된 종목에 대해 뉴스 검색 수행
파이션 코드 (참조하세요)
import requests
from bs4 import BeautifulSoup
import pandas as pd
import re
from datetime import datetime
class ReportFetcher:
def __init__(self):
self.today = datetime.today().strftime('%Y-%m-%d')
self.url = f"https://consensus.hankyung.com/analysis/list?skinType=business&sdate={self.today}&edate={self.today}&pagenum=80"
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
def fetch(self):
try:
response = requests.get(self.url, headers=self.headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
table_div = soup.find("div", {"class": "table_style01"})
if not table_div:
return pd.DataFrame()
table = table_div.find('table')
if not table:
return pd.DataFrame()
headers = [th.get_text(strip=True) for th in table.select('thead th')]
rows = []
for tr in table.select('tbody tr'):
cols = [td.get_text(strip=True) for td in tr.find_all('td')]
if cols:
rows.append(cols)
df = pd.DataFrame(rows, columns=headers)
return df
except Exception as e:
print(f"❌ 오류 발생: {e}")
return pd.DataFrame()
def filter_buy_reports(self, df):
required_columns = ['투자의견', '적정가격', '제목']
for col in required_columns:
if col not in df.columns:
print(f"❌ 누락된 컬럼: {col}")
return pd.DataFrame()
df = df[df['투자의견'].str.contains("매수|Buy", case=False, na=False)].copy()
df = df[df['적정가격'].notna() & (df['적정가격'] != "0") & (df['적정가격'] != "")]
df['기업명'] = df['제목'].apply(lambda x: re.search(r"^(.*?)\(", x).group(1) if re.search(r"^(.*?)\(", x) else "")
df['종목코드'] = df['제목'].apply(lambda x: re.search(r"\((\d{6})\)", x).group(1) if re.search(r"\((\d{6})\)", x) else "")
df['검색어'] = df['기업명'] + " (" + df['종목코드'] + ")"
return df[['검색어', '적정가격']]
python ReportFetCher.py
📊 매수 의견 리포트:
검색어 적정가격
0 GS건설 (006360) 26,000
1 현대로템 (064350) 280,000
3 팬오션 (028670) 5,500
4 HD현대 (267250) 260,000
6 HD현대중공업 (329180) 770,000
7 이녹스첨단소재 (272290) 34,000
8 HD현대마린엔진 (071970) 115,000
9 HD현대미포 (010620) 320,000
10 S-Oil (010950) 92,000
11 SK가스 (018670) 340,000
13 SK가스 (018670) 340,000
14 현대로템 (064350) 340,000
15 한화에어로스페이스 (012450) 1,380,000
16 S-Oil (010950) 90,000
17 HD현대 (267250) 309,000
19 현대로템 (064350) 290,000
20 팬오션 (028670) 5,000
21 한솔제지 (213500) 11,000
22 한화에어로스페이스 (012450) 1,280,000
23 S-Oil (010950) 90,000
24 녹십자 (006280) 200,000
25 현대로템 (064350) 30,000
🔍 뉴스 검색 시작: GS건설 (006360)
✅ 뉴스 검색 성공
🔗 [1/50] (2.0%) 링크 처리 중: http://www.queen.co.kr/news/articleView.html?idxno=445150
🔗 [2/50] (4.0%) 링크 처리 중: https://n.news.naver.com/mnews/article/011/0004551806?sid=101
🔗 [3/50] (6.0%) 링크 처리 중: http://www.newsprime.co.kr/news/article.html?no=710236
🔗 [4/50] (8.0%) 링크 처리 중: http://www.newstomato.com/ReadNews.aspx?no=1280263&inflow=N