我想從bs4/Python 3中使用BeautifulSoup來提取CData。但是,每當我使用下面的代碼搜索它時,它都會返回一個空的結果。任何人都可以指出我做錯了什麼?使用BeautifulSoup提取CData
from bs4 import BeautifulSoup,CData
txt = '''<foobar>We have
<![CDATA[some data here]]>
and more.
</foobar>'''
soup = BeautifulSoup(txt)
for cd in soup.findAll(text=True):
if isinstance(cd, CData):
print('CData contents: %r' % cd)
在答案[here](http://stackoverflow.com/questions/2032172/how-can-i-grab-cdata-out-of-beautifulsoup)中找到了類似的代碼,這是我認爲你得到的地方這個。 –
可能的重複[可以CDATA部分由BeautifulSoup保存嗎?](http://stackoverflow.com/questions/16426507/can-cdata-sections-be-preserved-by-beautifulsoup) – user985366