• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

출력값에 None은 왜 나오는건가요?

19.09.21 20:53 작성 조회수 552

0

안녕하세요

출력값에 제가 원하는 값이 나오기는 하는데 None이라는 것도 같이 출력되서요.. 혹시 왜 그런건가요?

<div class="course_title">모두를 위한 딥러닝 - 기본적인 머신러닝과 딥러닝 강좌</div>
None
<div class="course_title">R로 하는 웹크롤링 - 입문편</div>
None
<div class="course_title">누구나 할 수 있는 안드로이드 앱 개발 - 2 (Kotlin)</div>
None
None
None

그리고 print(e.select_one("div.card-content > div"))에서 뒤에 print(e.select_one("div.card-content > div").string) 으로 하면 

AttributeError: 'NoneType' object has no attribute 'string'

이런 에러가 나는데 잘 모르겠네요 ㅠㅜ

from bs4 import BeautifulSoup

import urllib.request as req

import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

url = "https://www.inflearn.com/"

res = req.urlopen(url).read()

soup = BeautifulSoup(res, "html.parser")

recommand = soup.select("div.columns.is-mobile")

for e in recommand:

    print(e.select_one("div.card-content > div"))

답변 1

답변을 작성해보세요.

0

네 안녕하세요.

for 문에서 없는 노드를 불러오기 때문에 None이 출력되는 것입니다.

if 문 등으로 처리해주시면 됩니다.

감사합니다.