0

我正在尝试解析一些经常有这样的片段的 html 文件:

<p class="p5"><p class="s2">In the directory </p>/home/blah/<p class="s2"> there is a file, </p>plotData.dat</p>

它被解析为:

>>> [c for c in P.body.children]
[<p class="p5"></p>,
 <p class="s2">In the directory </p>,
 u'/home/blah/',
 <p class="s2"> there is a file, </p>,
u'plotData.dat']

我希望它会像

>>> [c for c in P.body.children]
[<p class="p5"></p>,
 <p class="s2">In the directory </p>,
 <p class="p5">u'/home/blah/'</p>,
 <p class="s2"> there is a file, </p>,
 <p class="p5">u'plotData.dat'</a>]

那么输入的html格式是否错误?我可以做些什么来使输入的html解析为后者?(我无法控制 html 的外观)

编辑:完成 MWE:

>>> from bs4 import BeautifulSoup as BS
>>> P = BS('<p class="p5"><p class="s2">In the directory </p>/home/blah/<p class="s2"> there is a file, </p>plotData.dat</p>')
>>> [c for c in P.body.children]
[<p class="p5"></p>, <p class="s2">In the directory </p>, u'/home/blah/', <p class="s2"> there is a file, </p>, u'plotData.dat']
4

0 回答 0