Python去除空白美丽汤和条

标签 python beautifulsoup

我有一封 html 电子邮件,我使用 beautiful soup 提取了文本,然后我想删除任何前导空格,但无论我尝试 textwrap.dedent 或 string.strip() 多少次,它都不会不删除某些行中的空格。我已经完成了 print repr(string) ,输出是这样的。

\r\n   content

这意味着\r 和行上的内容之间存在实际的空格,即使我使用 strip 或其他任何东西来删除它们,这些行上仍然有空格。我该如何处理这个问题?

现在的代码:

no_html = BeautifulSoup(message).get_text()
final_message = no_html.strip()
print final_message

最佳答案

这对我有用

no_html.rstrip().strip()

从我的代码中删除了所有“/t/n”分隔符

关于Python去除空白美丽汤和条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597462/

相关文章:

python - 没有名为 googlesamples.assistant.auth_helpers 的模块

python - 在 Pandas 中合并 Dataframe block

python - 找到数组中上下最接近的值?

python - 如何在 Eclipse/Python/Appengine 中使用持久本地 NDB 数据进行调试?

Python 美汤 : Target a particular element

python - 读取 MHTML 文件

python - Django 模板中的嵌套点查找

Python 使用 Beautiful Soup 对特定内容进行 HTML 处理

python - 如何填写 html 表单并从网站上抓取?

python - 无法从 shopee.com 抓取最畅销的产品