강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

frenchkebab님의 프로필 이미지
frenchkebab

작성한 질문수

파이썬 입문 및 웹 크롤링을 활용한 다양한 자동화 어플리케이션 제작하기

BeautifulSoup 사용법 및 간단 웹 파싱 실습(1) - 네이버, 다음, 인프런

질문있습니다

작성

·

182

0

다음 코드에서 string을 안붙이면 정상적으로 출력이 되는데, string을 붙이면 None을 출력합니다.

왜때문인거죠?

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://finance.daum.net/"
res = req.urlopen(url).read()
soup = BeautifulSoup(res, "html.parser")
top = soup.select("#wrap > div.footer > span > div.fl > p")[0]

print(top.string)

 

답변 1

0

좋은사람님의 프로필 이미지
좋은사람
지식공유자

안녕하세요. frenchkebab 님 좋은 질문입니다.

우선 위 질문과 똑같은 질문을 한 링크가 있어서 연결해 드립니다.

https://stackoverflow.com/questions/20750852/beautifulsoup-4-python-string-returns-none

답변중에 

.string returns None because the text node is not the only child (there is a comment).

마지막으로 가져온 top 하위에는 더이상 텍스트 노드가 자식이 아니기 때문에 None 이 출력되는 것입니다.

 

쉽게 사용하실려면 일반적으로 select 로 리턴받은 text노드를 찾아서 있는 그대로 출력해주시면 됩니다.

위에 답변을 번역해서 보시면 좀 더 이해가 빠릅니다.

 

 

frenchkebab님의 프로필 이미지
frenchkebab

작성한 질문수

질문하기