크롤링이 무엇인지도 이해가 잘 안되서 “크롤링을 왜 해야 하는지” 부터 GPT에 물어봤었습니다. csv 파일이란 것도 선구자분들 덕분에 알게 되었습니다. 감사합니다.
계속 헤매다가 결국 다른 분들 코드 참조하여 한번 성공했네요…😵💫
주제: “동탄 부동산”에 대한 기사 크롤링하고 csv 파일로 출력하기
import requests
from bs4 import BeautifulSoup
import pandas as pd
# URL for the Naver News search results
url = "https://search.naver.com/search.naver?where=news&ie=utf8&sm=nws_hty&query=%EB%8F%99%ED%83%84+%EB%B6%80%EB%8F%99%EC%82%B0"
# Send a GET request to the URL
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Parsing the news articles
articles = soup.select('div.news_wrap.api_ani_send')
# List to hold the news data
news_data = []
for article in articles:
title = article.select_one('a.news_tit').text.strip()
link = article.select_one('a.news_tit')['href']
source = article.select_one('a.info.press').text.strip()
date = article.select_one('span.info').text.strip()
description = article.select_one('a.api_txt_lines.dsc_txt_wrap').text.strip()
news_data.append([title, link, source, date, description])
# Create a DataFrame
df = pd.DataFrame(news_data, columns=['Title', 'Link', 'Source', 'Date', 'Description'])
# Save the DataFrame to a CSV file with utf-8-sig encoding
csv_path = 'dongtan_real_estate_news.csv'
df.to_csv(csv_path, index=False, encoding='utf-8-sig')
print(f"CSV file saved at: {csv_path}")
블룸버그 뉴스사이트에 들어가서 시도해봤는데, 전혀 출력이 안되네요…
여기까지만 해보고 더 배우겠습니다😶