File size: 1,081 Bytes
86797a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ff16ac
b92961a
7ff16ac
b92961a
 
 
 
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
36
37
38
39
40
41
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)