解析python中的HTML wxWidgets TextCtrl

时间:2011-10-13 22:16:39

标签: python wxpython wxwidgets

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

2 个答案:

答案 0 :(得分:1)

当然,只需使用myTextCtrl.GetValue(),然后使用BeautifulSoupxml.dom.minidomHTMLParser等内容解析字符串:

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>

答案 1 :(得分:0)

wxTextCtrl将显示包含所有标记的HTML

<html><body>Hello, world!</body></html>");

要渲染html,您需要使用wxHtmlWindow

w = wxHtmlWindow(this)
w.SetPage("<html><body>Hello, world!</body></html>")