
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
requests, beautifulsoup, pandas를 사용하여 Tesla의 재무제표를 데이터프레임으로 보여주는 python 코드를 작성해줘
라고 질문을 해서 다음과 같이 나왔어요
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Tesla의 재무제표 페이지 URL
url = "https://finance.yahoo.com/quote/TSLA/financials"
# 페이지 요청
response = requests.get(url)
# BeautifulSoup을 사용하여 HTML 파싱
soup = BeautifulSoup(response.text, 'html.parser')
# 재무제표 테이블 찾기
table = soup.find('div', class_='M(0) Whs(n) BdEnd Bdc($seperatorColor) D(itb)')
# 테이블 데이터 추출
data = []
for row in table.find_all('div', class_='D(tbr)'):
cols = row.find_all('div')
row_data = [col.text for col in cols]
data.append(row_data)
# 데이터프레임 생성
df = pd.DataFrame(data[1:], columns=data[0])
# 결과 출력
print(df)
그런데 아래와 같이 에러가 나요.
AttributeError Traceback (most recent call last)
<ipython-input-28-4e9350321417> in <cell line: 21>()
19 # 테이블 데이터 추출
20 data = []
---> 21 for row in table.find_all('div', class_='D(tbr)'):
22 cols = row.find_all('div')
23 row_data = [col.text for col in cols]
AttributeError: 'NoneType' object has no attribute 'find_all'
아무래도 외국기업이라 네이버금융이 아니다 보니까 그런거 같은데 해결방법이 있을까요?
