인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

tubowangin4195's profile image
tubowangin4195

asked

Introduction to Python and Creating Various Automated Applications Using Web Crawling

How to use BeautifulSoup and Simple Web Parsing Practice (1) - Naver, Daum, Inflearn

질문있습니다

Written on

·

170

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)

 

python웹-크롤링

Answer 1

0

niceman님의 프로필 이미지
niceman
Instructor

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

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

 

 

tubowangin4195's profile image
tubowangin4195

asked

Ask a question