在 python wxWidgets TextCtrl 中解析 HTML

发布于 2024-12-10 02:09:30 字数 52 浏览 0 评论 0原文

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

Is it possible, or is there a library that will allow me to parse HTML code inside the wx.TextCtrl widget?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溇涏 2024-12-17 02:09:30

当然,只需使用 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>

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>
冬天的雪花 2024-12-17 02:09:30

wxTextCtrl将显示带有所有标签的HTML

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

要渲染html,您需要使用wxHtmlWindow

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

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>")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文