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(150):
    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
블로그 이미지

Link2Me

,