커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
1-9 재무제표 불러오기 ChatGPT 크롤링 프롬프트 관련
[왕초보] ChatGPT 300% 활용하기 v0
1주차
북마크
김*린
댓글
8
추천
0
조회수
15
조회수
15
답변 완료

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

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


프롬프트대로 쳤을 때 버전이 달라서인지 뜨는 결과값이 다른데, 이 코드를 그대로 파이썬에 붙여넣고 실행하면 에러가 뜹니다. 어떻게 고쳐야 실행이 되는지 궁금합니다.



import requests

from bs4 import BeautifulSoup

import pandas as pd


def get_samsung_financial_statement():

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

    # 네이버 증권 페이지에서 삼성전자의 재무제표 페이지로 이동합니다.

    response = requests.get(url)

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

   

    # 재무제표 데이터를 담고 있는 테이블을 찾습니다.

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

   

    # 테이블의 첫 번째 행은 열 이름으로 사용합니다.

    columns = [th.text.strip() for th in table.select('thead th')]

   

    # 테이블의 나머지 행은 데이터로 사용합니다.

    data = []

    for row in table.select('tbody tr'):

        data.append([td.text.strip() for td in row.find_all('td')])

   

    # 데이터프레임을 생성합니다.

    df = pd.DataFrame(data, columns=columns)

    return df


# 삼성전자의 재무제표 데이터프레임을 출력합니다.

print(get_samsung_financial_statement())




작성한 코드 및 에러 메세지

---------------------------------------------------------------------------
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:


6 frames

AssertionError: 23 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: 23 columns passed, passed data had 10 columns

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

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

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




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