반응형
Part 3. PYTHON _ 데이터 분석 프로젝트
04. 파이썬 데이터 분석 프로젝트
8. 뉴스기사 키워드 추출을 통한 주식 종목 이슈 분석
1) 데이터 수집
- 주식 데이터 수집
import FinanceDataReader as fdr
kospi = fdr.StockListing('KOSPI')
kospi.head()
stock_close = pd.DataFrame()
for i in top3.index:
temp = fdr.DataReader(top3.loc[i,'Code'], start='2022-01-01', end='2022-12-31')[['Close']].rename({'Close':top3.loc[i,'Name']}, axis=1)
stock_close = pd.concat([stock_close, temp], axis=1)
- 일자별 각 종목 뉴스 제목 수집
date_list = list(stock_close.index.strftime('%Y.%m.%d'))
print(date_list)
def news_crawling(keyword):
data = pd.DataFrame()
for dt in tqdm(date_list):
dt1 = dt.replace('.','')
url = f'https://search.naver.com/search.naver?where=news&query={keyword}&sm=tab_opt&sort=0&photo=0&field=0&pd=3&ds={dt}&de={dt}&docid=&related=0&mynews=0&office_type=0&office_section_code=0&news_office_checked=&nso=so%3Ar%2Cp%3Afrom{dt1}to{dt1}&is_sug_officeid=0&office_category=0&service_area=1'
user_agent = generate_user_agent()
headers = {'User-Agent':user_agent}
res = requests.get(url, headers=headers)
time.sleep(random.random())
soup = bs(res.text, 'html.parser')
title = [i.text for i in soup.find_all('a', class_='news_tit')]
if len(title) < 1 : #크롤링이 아무것도 되지 않았으면 루프를 종료, 시간 텀을 가진 뒤 다시 크롤링
break
temp = pd.DataFrame({'keyword':keyword, 'title':title, 'date_ymd':dt1})
data = pd.concat([data, temp])
return data
keyword_list = ['삼성전자','LG에너지솔루션','SK하이닉스']
news_data = pd.DataFrame()
for k in keyword_list:
news = news_crawling(k)
news_data = pd.concat([news_data, news])
1) 데이터 전처리
- 형태소 분석
news_data['Noun'] = news_data['title'].apply(lambda x: [i[0] for i in mecab.pos(x) if i[1] in ("NNG","NNP") and len(i[0]) > 1])
news_data['date_ym'] = [str(i)[:6] for i in news_data['date_ymd']]
news_keyword = news_data.groupby(['keyword','date_ym'])[['Noun']].sum().reset_index()
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다.
패스트캠퍼스 [직장인 실무교육]
프로그래밍, 영상편집, UX/UI, 마케팅, 데이터 분석, 엑셀강의, The RED, 국비지원, 기업교육, 서비스 제공.
fastcampus.co.kr
반응형
'Challange' 카테고리의 다른 글
패스트캠퍼스 환급챌린지 53일차 미션 (3월 24일) : 데이터 분석 Master Class 강의 후기 (1) | 2024.03.24 |
---|---|
패스트캠퍼스 환급챌린지 52일차 미션 (3월 23일) : 데이터 분석 Master Class 강의 후기 (0) | 2024.03.23 |
패스트캠퍼스 환급챌린지 50일차 미션 (3월 21일) : 데이터 분석 Master Class 강의 후기 (0) | 2024.03.21 |
패스트캠퍼스 환급챌린지 49일차 미션 (3월 20일) : 데이터 분석 Master Class 강의 후기 (0) | 2024.03.20 |
패스트캠퍼스 환급챌린지 48일차 미션 (3월 19일) : 데이터 분석 Master Class 강의 후기 (0) | 2024.03.19 |