【问题标题】:Get rid of ­ in beautifulsoup在beautifulsoup 中摆脱
【发布时间】:2021-09-01 12:38:35
【问题描述】:

我想删除 ­ 部分。 (&shy 是一些编码字符集中保留的代码点,目的是通过插入可见连字符来跨行分隔单词。here) 但是要删除它,我的代码编辑器和 bs4 都无法识别它。 例子: html示例代码代码:

<span class="lemma__main">Mut&shy;­ter</span>

Python 代码:

soup = BeautifulSoup(htmlexample, 'html.parser')
print(soup.find('span', class_='lemma__main').text)

返回:

Mut<0xad>ter

注意: 指的是崇高文本 3 中的空格,但与我尝试替换字符串中的空格时不完全相同, 它只是不识别为一个。 在解释器中它只是一个空格

response.replace(' ', '')

如果我尝试在其中替换 &shy,它也不起作用:

response.replace('&shy','')

都返回:

Mut<0xad>ter

我已经查过了here给出的解决方案

你能帮我解决这个问题吗?

【问题讨论】:

  • print(soup.find('span', class_='lemma__main').text.replace('\xad', '')) 为我工作。
  • 它有效,谢谢

标签: python html beautifulsoup


【解决方案1】:
from bs4 import BeautifulSoup

html = '<span class="lemma__main">Mut&shy;­ter</span>'

soup = BeautifulSoup(html, 'lxml').prettify(
    formatter=lambda x: x.replace(u'\xad', ''))
print(soup)

输出:

<html>
 <body>
  <span class="lemma__main">
   Mutter
  </span>
 </body>
</html>

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多