
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
강의 내용대로 ChatGPT 에서 삼성전자 재무제표를 불러오는 코드를 실행하였는데 에러가 발생하여 문의 드립니다.

작성한 코드 및 에러 메세지
입력한 코드 (ChatGPT 답변)
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 삼성전자 종목 코드
stock_code = '005930'
# 네이버 금융 URL 설정
url = f"https://finance.naver.com/item/main.nhn?code={stock_code}"
# HTTP 요청을 보내고 응답을 받아서 파싱
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 재무제표 테이블 찾기
finance_table = soup.select_one('div.section.cop_analysis div.sub_section')
tables = finance_table.select('table')
# 연간 재무제표 데이터 추출
annual_data = []
header = [th.get_text().strip() for th in tables[1].select('thead th')]
for row in tables[1].select('tbody tr'):
columns = row.select('th') + row.select('td')
annual_data.append([col.get_text().strip() for col in columns])
# 분기 재무제표 데이터 추출
quarterly_data = []
for row in tables[2].select('tbody tr'):
columns = row.select('th') + row.select('td')
quarterly_data.append([col.get_text().strip() for col in columns])
# 데이터프레임 생성
annual_df = pd.DataFrame(annual_data, columns=header)
quarterly_df = pd.DataFrame(quarterly_data, columns=header)
# 출력
print("연간 재무제표")
print(annual_df)
print("\n분기 재무제표")
print(quarterly_df)
출력된 오류 메세지
<IndexError Traceback (most recent call last)
<ipython-input-31-aa4c543c9af9> in <cell line: 21>()
19 # 연간 재무제표 데이터 추출
20 annual_data = []
---> 21 header = [th.get_text().strip() for th in tables[1].select('thead th')]
22 for row in tables[1].select('tbody tr'):
23 columns = row.select('th') + row.select('td')
IndexError: list index out of range
