python - 如何从html标签之间提取文本?
问题描述
我有一些html
要从中提取文本的元素。所以html
就像
<pre>
<span class="ansi-red-fg">ZeroDivisionError</span>Traceback (most recent call last)
<span class="ansi-green-fg"><ipython-input-2-0f9f90da76dc></span> in <span class="ansi-cyan-fg"><module></span><span class="ansi-blue-fg">()</span>
</pre>
我想将文本提取为
ZeroDivisionErrorTraceback (most recent call last)
<ipython-input-2-0f9f90da76dc> in<module>()
我在这里找到了该问题的答案,但它对我不起作用。完整的示例代码
from bs4 import BeautifulSoup as BSHTML
bs = BSHTML("""<pre>
<span class="ansi-red-fg">ZeroDivisionError</span>Traceback (most recent call last)
<span class="ansi-green-fg"><ipython-input-2-0f9f90da76dc></span> in <span class="ansi-cyan-fg"><module></span><span class="ansi-blue-fg">()</span>
</pre>""")
print bs.font.contents[0].strip()
我收到以下错误:
Traceback (most recent call last):
File "invest.py", line 13, in <module>
print bs.font.contents[0].strip()
AttributeError: 'NoneType' object has no attribute 'contents'
有什么我想念的吗?版本beautifulsoap
:4.6.0
解决方案
Do you want all the text content of that pre
block?
print bs.pre.text
Returns:
ZeroDivisionErrorTraceback (most recent call last)
<ipython-input-2-0f9f90da76dc> in <module>()
推荐阅读
- python - Beautiful Soup 没有提供任何东西,尽管存在价值
- java - Spark rdd 使用 collect 方法生成 OutOfMemoryError
- jquery - 如何使用 Vue.js 将 DataTable() 与数组中的数据(v-for 循环)附加到现有 HTML?
我在 Vue.js 中遇到问题,无法将我的表设为 DataTable(带有搜索字段、排序列等)。
datatable.min.js
我在里面调用原始文件,<head>
或者拥有并调用另一个custom.js
文件:$(documen
- typescript - 工厂函数的命名返回类型?
- java - Hybris 关系灵活搜索选择
- android - 当我使用“tns preview”启动安卓模拟器时,我得到一个错误代码
- powershell - 自动下载累积更新的过程
- php - 如何在循环中创建新的 php 对象?
- firebase - Firebase 功能在没有任何解释或错误的情况下消失了
- php - 如何在wordpress中调整块的大小?