Skip to main content
 首页 » 编程设计

python之在 python wxWidgets TextCtrl 中解析 HTML

2025年05月04日12freeliver54

是否可能,或者是否有一个库可以让我在 wx.TextCtrl 小部件中解析 HTML 代码?

请您参考如下方法:

当然,只需使用 myTextCtrl.GetValue(),然后用 BeautifulSoup 之类的东西解析字符串, xml.dom.minidom , HTMLParser等:

from BeautifulSoup import BeautifulSoup 
 
# lets say this is the text inside the TextCtrl: 
# '<html><head><title>Page title</title></head><body><p id="firstpara" align="center">This is paragraph <b>one</b>.<p id="secondpara" align="blah">This is paragraph <b>two</b>.</html>' 
# 
 
htmlStr = myTextCtrl.GetValue() 
 
soup = BeautifulSoup(htmlStr) 
soup.contents[0].name 
# u'html' 
 
soup.contents[0].contents[0].name 
# u'head' 
 
head = soup.contents[0].contents[0] 
head.parent.name 
# u'html' 
 
head.next 
# <title>Page title</title> 
 
head.nextSibling.name 
# u'body' 
 
head.nextSibling.contents[0] 
# <p id="firstpara" align="center">This is paragraph <b>one</b>.</p> 
 
head.nextSibling.contents[0].nextSibling 
# <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>