커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
3-8 웹스크롤링 코드 순서 문의
undefined주차
북마크
남*걸
댓글
3
추천
0
조회수
12
조회수
12
답변 완료

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

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


강의중 네이버 영화 웹페이지 순위 / 제목 / 평점 스크랩핑 시 순위에 대해서 태크 접근하여 스프랭핑 할때 코드 순서에 따라 에러가 뜨게 되는데


이유는 알기 어려워 문의 드립니다.





작성한 코드 및 에러 메세지

정상 동작 코드

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)


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