python - 使用 BeautifulSoup 删除 td 元素之间/之后的空格

标签 python html beautifulsoup

有人知道如何使用 beautiful soup 删除 td 元素之间的空格吗?

例如

<table>
  <tr class="soup-target">
    <td></td>     <td></td>
  </tr>
</table>

在你说“只需使用删除键”之前,这是不可能的,因为我使用的模板语言在 td 元素上有一个循环,并且该语言不允许控制空间循环元素上的空格或换行符。

最佳答案

您还可以直接过滤tr内的文本节点并提取它们:

row = soup.find("tr", class_="soup-target")
for text_node in row.find_all(text=True, recursive=False):
    text_node.extract()

关于python - 使用 BeautifulSoup 删除 td 元素之间/之后的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34667537/

相关文章:

python - 删除python中两个html标签之间的所有数据

python - TensorFlow中有没有相当于python的reduce函数?

python - 正则表达式: "don' t 返回其余部分“if "this condition"?

python - 修改具有相同键的字典列表

html - 使用 100% HEIGHT 和 RELATIVE DIV 定位

python - 如何使用BeautifulSoup访问标签的属性值

Python/BeautifulSoup - 如何提取 <li> 和 <dl> 标签之间的文本

Python Button 绑定(bind)两个函数并使用绑定(bind)发送参数

html - 为什么我的 HTML 不使用 CSS 中定义的最后一个样式?

c# - 在右边<TD>的右边显示左边的<TD>