Spaces:
Sleeping
Sleeping
File size: 912 Bytes
86797a6 |
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 31 32 33 34 35 |
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)
|