728x90
네이버 증권 뉴스를 엑셀로 저장하는 코드 예제이다.
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
|
import requests
from bs4 import BeautifulSoup
import pandas as pd
from datetime import datetime # Importing datetime module
today = datetime.now().strftime("%Y-%m-%d")
data = []
for i in range(1, 50):
url = f"https://finance.naver.com/news/mainnews.naver?date={today}&page={i}"
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
articles = soup.select(".block1")
for article in articles:
title = article.select_one(".articleSubject > a").text
link = "https://finance.naver.com" + article.select_one(".articleSubject > a").get("href")
content = article.select_one(".articleSummary").contents[0].strip()
press = article.select_one(".press").text.strip()
date = article.select_one(".wdate").text.strip()
data.append([title, link, content, press, date])
if soup.select_one(".pgRR") is None:
break
df = pd.DataFrame(data, columns=['제목','링크','내용','언론사','날짜'])
df.to_csv("naver_finance_news.csv", index=False, encoding="utf-8-sig") # UTF-8 인코딩 설정
|
728x90
'Web 크롤링 > Python Crawling' 카테고리의 다른 글
서울 중구청 조직도 크롤링 Python 코드 (1) | 2025.03.16 |
---|---|
강동구청 조직도 크롤링 (0) | 2025.03.02 |
종로구청 조직도 크롤링 (0) | 2025.03.01 |
[Python] 종로구청 동주민센터 크롤링 (0) | 2025.02.18 |
정부(행정안전부) 주소 검증 (0) | 2024.04.20 |