커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
타입 에러 질문 드립니다.
undefined주차
북마크
손*수
댓글
3
추천
1
조회수
6
조회수
6
답변 완료

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

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


trs = soup.select('#old_content > table > tbody > tr')
for tr in trs:
    rank = tr.select_one('td:nth-child(1) > img')
    title = tr.select_one('td.title > div > a')
    point = tr.select_one('td.point')
    if title is not None:
        print(rank['alt'],title.text,point.text)


위와 같은 코드에서 강의 내용에 따라 수정 하였을 때 타입 에러가 발생했습니다.

rank = tr.select_one('td:nth-child(1) > img')
...
 print(rank['alt'],title.text,point.text)


위와 같은 코드에서 ['alt'] 출력 할 때에 배열 인덱스를 호출 하는 건 문제가 되지 않았으나

rank = tr.select_one('td:nth-child(1) > img')['alt']
...
 print(rank,title.text,point.text)

강의 내용에 따라서 rank 변수를 선언 할 때에 ['alt'] 의 구성원을 삽입하여 타입 에러가 발생했습니다.


전체 코드

import requests
from bs4 import BeautifulSoup


headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)


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


#타이틀
#old_content > table > tbody > tr:nth-child(2) > td.title > div > a


#점수
#old_content > table > tbody > tr:nth-child(2) > td.point


#순위
#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img


trs = soup.select('#old_content > table > tbody > tr')
for tr in trs:
    rank = tr.select_one('td:nth-child(1) > img')['alt']
    title = tr.select_one('td.title > div > a')
    point = tr.select_one('td.point')
    if title is not None:
        print(rank,title.text,point.text)


스파르타 즉문즉답


스파르타 즉문즉답




son@DESKTOP-QNELLUK MINGW64 ~/Desktop/starta/pythonprac
$ c:/Users/son/Desktop/starta/pythonprac/venv/Scripts/python.exe c:/Users/son/Desktop/starta/pythonprac/hello.py
Traceback (most recent call last):
  File "c:/Users/son/Desktop/starta/pythonprac/hello.py", line 20, in <module>
    rank = tr.select_one('td:nth-child(1) > img')['alt']
TypeError: 'NoneType' object is not subscriptable
(venv) 
son@DESKTOP-QNELLUK MINGW64 ~/Desktop/starta/pythonprac
$



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