2017-12-20 5 views
2

私はbeautifulsoupを使ってevernoteチェックリストの内容を解析しようとしています。しかし、内容のhtmlパーサを呼び出すと、self-closingタグ(en-todo)を修正し続けるので、en-todoタグのテキストを取得しようとすると空白になります。Python BeautifulSoup:self-closingタグのテキストを取得する方法

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'] 
関連する問題