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() # 이미지가 있는지 확인 img_tag = news_item.find('img') image_url = img_tag['src'] if img_tag else '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)