
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
강의중 네이버 영화 웹페이지 순위 / 제목 / 평점 스크랩핑 시 순위에 대해서 태크 접근하여 스프랭핑 할때 코드 순서에 따라 에러가 뜨게 되는데
이유는 알기 어려워 문의 드립니다.
작성한 코드 및 에러 메세지
정상 동작 코드
for tr in trs:
title=tr.select_one('td.title > div > a')
point=tr.select_one('td.point')
rank=tr.select_one('td:nth-child(1) > img')
if title is not None:
print(rank['alt'], title.text, point.text)
에러 발생 코드
rank 스크랩핑시 alt정보를 if문 진입전 rank 변수 선언 및 직접 접근하면 에러발생
for tr in trs:
title=tr.select_one('td.title > div > a')
point=tr.select_one('td.point')
rank=tr.select_one('td:nth-child(1) > img')['alt']
if title is not None:
print(rank, title.text, point.text)
단, if문 안에서 진입 후 rank 변수 선언 및 직접 접근하면 에러 미발생. (정상 동작)
for tr in trs:
title=tr.select_one('td.title > div > a')
point=tr.select_one('td.point')
rank=tr.select_one('td:nth-child(1) > img')
if title is not None:
rank=tr.select_one('td:nth-child(1) > img')['alt']
print(rank, title.text, point.text)
