Spaces:
Sleeping
Sleeping
import requests | |
from bs4 import BeautifulSoup | |
import pandas as pd | |
# URL 설정 | |
url = "https://news.naver.com/main/ranking/popularDay.naver" | |
# 요청 보내기 | |
response = requests.get(url) | |
soup = BeautifulSoup(response.content, 'html.parser') | |
# 뉴스 리스트 추출 | |
news_list = [] | |
for news_item in soup.select('div.rankingnews_box ul.rankingnews_list li'): | |
rank = news_item.find('em', class_='list_ranking_num').text | |
title = news_item.find('a', class_='list_title').text.strip() | |
link = news_item.find('a', class_='list_title')['href'] | |
time = news_item.find('span', class_='list_time').text.strip() | |
image_url = news_item.find('img')['src'] | |
news_list.append({ | |
'Rank': rank, | |
'Title': title, | |
'Link': link, | |
'Time': time, | |
'Image URL': image_url | |
}) | |
# 데이터프레임으로 변환 | |
df = pd.DataFrame(news_list) | |
# 결과 출력 | |
print(df) | |