• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

질문있습니다

19.06.04 20:58 작성 조회수 112

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노드를 찾아서 있는 그대로 출력해주시면 됩니다.

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