2017-12-20 5 views
2

my는 beautifulsoup를 사용하여 evernote 체크리스트의 내용을 파싱하려고합니다. 하지만 내용에 html 파서를 호출하면 자체 닫기 태그 (en-todo)가 계속 수정되므로 en-todo 태그의 텍스트를 얻으려고하면 해당 태그가 비어 있습니다.Python BeautifulSoup : 자기 닫는 태그의 텍스트를 얻는 방법

note_body = '<en-todo checked="true" />window caulk<en-todo />cake pan<en-todo />cake mix<en-todo />salad mix<en-todo checked="true"/>painters tape<br />' 

import re 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(note_body, 'html.parser') 
checklist_items = soup.find_all('en-todo') 
print checklist_items 

위의 코드는 텍스트가없는 태그 만 반환합니다.

이것은 당신의 문자열을 작동
[<en-todo checked="true"></en-todo>, <en-todo></en-todo>, <en-todo></en-todo>, <en-todo></en-todo>, <en-todo checked="true"></en-todo>] 

답변

0

:

from bs4 import BeautifulSoup 
soup = BeautifulSoup(note_body, 'html.parser') 
checklist_items = soup.find_all('en-todo') 
for item in checklist_items: 
    print(item.get_text()) 
0

당신은 태그로 묶여있는 문자 메시지를 얻을 필요가!

tag.next_sibling을 사용해야합니다!

>>> [each.next_sibling for each in checklist_items] 
[u'window caulk', u'cake pan', u'cake mix', u'salad mix', u'painters tape'] 
관련 문제