커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
GPT 가 생성해준 코드인데, 수행이... 이상해요.
undefined주차
북마크
전*무
댓글
2
추천
0
조회수
20
조회수
20
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


프롬프트는 수업 자료 그대로

네이버 금융 사이트에서 삼성전자 재무제표를 크롤링해서 데이터프레임으로 보여주는 Python 코드를 작성해줘

라고 입력해서 GPT가 생성해준 대로 COLAB 에 입력했는데, 결과가 나오지 않습니다.





작성한 코드 및 에러 메세지

import requests

from bs4 import BeautifulSoup

import pandas as pd


# 삼성전자 재무제표 페이지 URL

url = 'https://finance.naver.com/item/main.naver?code=005930#'


# HTML 파싱

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')


# 재무제표 테이블 데이터 추출

table_data = []

table = soup.find('table', {'class': 'tb_type1 tb_num tb_type1_ifrs'})

thead = table.find('thead')

tr_head = thead.find_all('tr')

for tr in tr_head:

    th_data = [th.get_text().strip() for th in tr.find_all('th')]

    table_data.append(th_data)

tbody = table.find('tbody')

tr_body = tbody.find_all('tr')

for tr in tr_body:

    td_data = [td.get_text().strip() for td in tr.find_all('td')]

    table_data.append(td_data)


# 데이터프레임 생성

df = pd.DataFrame(table_data[1:], columns=table_data[0])


print(df)

=========================

COLAB 에서의 수행 결과는 아래와 같습니다.

+++++++++++++++++++++++

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/internals/construction.py in _finalize_columns_and_data(content, columns, dtype)
    968     try:
--> 969         columns = _validate_or_indexify_columns(contents, columns)
    970     except AssertionError as err:


5 frames

AssertionError: 3 columns passed, passed data had 10 columns
The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/internals/construction.py in _finalize_columns_and_data(content, columns, dtype)
    970     except AssertionError as err:
    971         # GH#26429 do not raise user-facing AssertionError
--> 972         raise ValueError(err) from err
    973 
    974     if len(contents) and contents[0].dtype == np.object_:

ValueError: 3 columns passed, passed data had 10 columns

오류 발생 시, 작성한 코드 전체와 에러 메시지를 첨부해 주세요.

Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.

Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!




취소
 공유
취소
댓글 0
댓글 알림
나의얼굴