其他类似题目的问题都没有回答我的问题.
如果我执行以下命令:
from bs4 import BeautifulSoup
soup = BeautifulSoup("<html><body><p>111</p><p>before<ul><li>222</li></ul>after</p></body></html>", "lxml")
soup.find_all(["p", "li"])
我得到的结果是:
[<p>111</p>, <p>before</p>, <li>222</li>]
我希望在结果中也能找到"After",或者作为第二个"p"元素的一部分,或者作为列表中的第四个项目.
Is this expected behaviour? Is there a way to retrieve the text "after"?个
更奇怪的是,如果我做print(soup.prettify())
,这就是结果.
<html>
<body>
<p>
111
</p>
<p>
before
</p>
<ul>
<li>
222
</li>
</ul>
after
</body>
</html>
"ul"和"After"不再是第二个"p"的一部分.我假设源文件不是有效的html(?),但再次声明:
Is there a way to deal with this, except from just dropping "after"?个