
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
Chatgpt에서 생성한 파이썬 코드가 실행시 오류납니다.
뭐가 문제일까요?

작성한 코드 및 에러 메세지
# Step 1: 필요한 라이브러리 설치 (필요 시)
!pip install requests
!pip install beautifulsoup4
!pip install pandas
# Step 2: 필요한 모듈 임포트
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Step 3: 삼성전자 재무제표 URL 설정
url = 'https://finance.naver.com/item/main.nhn?code=005930'
# Step 4: 웹 페이지 요청 및 HTML 파싱
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Step 5: 재무제표 데이터 추출
# 네이버 금융에서 재무제표 테이블을 추출
financial_data = []
# 네이버 금융의 재무제표 테이블은 'table' 태그에 'class' 속성으로 식별할 수 있습니다.
table = soup.find('table', {'class': 'tb_type1_ifrs'})
# 테이블에서 각 행 데이터를 추출
if table:
rows = table.find_all('tr')
for row in rows:
columns = row.find_all(['th', 'td'])
column_data = [col.get_text().strip() for col in columns]
financial_data.append(column_data)
# Step 6: 추출한 데이터로 데이터프레임 생성
# 헤더와 데이터 부분 분리
header = financial_data[0]
data = financial_data[1:]
# 데이터프레임 생성
df = pd.DataFrame(data, columns=header)
# Step 7: 결과 출력
print(df)
