parse HTML in python wxWidgets TextCtrl
Is it possible, or is there a library that will allow me to parse HTML code inside the 开发者_如何学运维wx.TextCtrl widget?
Sure, just use myTextCtrl.GetValue()
, then parse the string with something like BeautifulSoup
, xml.dom.minidom
, HTMLParser
, etc:
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>
wxTextCtrl will display the HTML with all the tags
<html><body>Hello, world!</body></html>");
To render the html, you need to use wxHtmlWindow
w = wxHtmlWindow(this)
w.SetPage("<html><body>Hello, world!</body></html>")
精彩评论