newsranking / app.py
CSB261's picture
Update app.py
b92961a verified
raw
history blame
1.08 kB
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()
# 이미지 태그와 src 속성 확인
img_tag = news_item.find('img')
if img_tag and 'src' in img_tag.attrs:
image_url = img_tag['src']
else:
image_url = 'No Image Available'
news_list.append({
'Rank': rank,
'Title': title,
'Link': link,
'Time': time,
'Image URL': image_url
})
# 데이터프레임으로 변환
df = pd.DataFrame(news_list)
# 결과 출력
print(df)