簡體   English   中英

如何從html標簽之間提取文本?

[英]How to extract text from between html tags?

我有一些html元素,我想從中提取文本。 所以html就像

<pre>
<span class="ansi-red-fg">ZeroDivisionError</span>Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-2-0f9f90da76dc&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</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">&lt;ipython-input-2-0f9f90da76dc&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</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

您是否需要該pre塊的所有文本內容?

print bs.pre.text

返回:

ZeroDivisionErrorTraceback (most recent call last)
<ipython-input-2-0f9f90da76dc> in <module>()

您的代碼示例中的.font引用HTML標記<font> 由於您正在查找文檔中的所有文本,因此可以使用以下內容:

contents = bs.find_all(text=True)
for c in contents:
    print(c)  # replace this with whatever you're trying to do

輸出:

ZeroDivisionError
Traceback (most recent call last)

<ipython-input-2-0f9f90da76dc>
 in
<module>
()

當前bs.fontNone因為您正在解析不包含任何<font>標記的文檔。

如果只想將內容作為一個長字符串,則只需使用bs.text

'\nZeroDivisionErrorTraceback (most recent call last)\n<ipython-input-2-0f9f90da76dc> in <module>()\n'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM