gpt4 book ai didi

python - 在 python wxWidgets TextCtrl 中解析 HTML

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:22 25 4
gpt4 key购买 nike

是否可能,或者是否有一个库可以让我在 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>

关于python - 在 python wxWidgets TextCtrl 中解析 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com