【问题标题】:Iterate over all elements in html and replace content with Beautifulsoup遍历 html 中的所有元素并用 Beautifulsoup 替换内容
【发布时间】:2021-10-08 22:57:55
【问题描述】:

在我的数据库中,我存储来自自定义 CMS 的 WYSIWYG 编辑器的 HTML。 内容是英文的,我想使用 Beautifulsoup 来遍历每个元素,将其内容翻译成德语(使用另一个类 Translator)并用翻译后的文本替换当前元素的值。

到目前为止,我已经能够为 p、a、pre 结合 Beautifulsoup 的 .findAll 功能提出特定的选择器,但是我已经用谷歌搜索过,我不清楚如何简单地完成所有操作元素并动态替换其内容,而不必根据特定类型进行过滤。

编辑器生成的涵盖所有不同类型的 HTML 的一个非常基本的示例:

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<p>Normal text</p>
<p><strong>Bold text</strong></p>
<p><em>Italic text </em></p>
<p><br></p>
<blockquote>Quote</blockquote>
<p>text after quote</p>
<p><br></p>
<p><br></p>
<pre class="code-syntax" spellcheck="false">code</pre>
<p><br></p>
<p>text after code</p>
<p><br></p>
<p><a href="https://google.com/" target="_blank">This is a search engine</a></p>
<p><br></p>
<p><img src="https://via.placeholder.com/350x150"></p>

bs4 文档将我指向replace_with 函数,如果我只能逐个选择每个元素,而不必专门选择某些内容,这将是理想的选择。

欢迎指点????

【问题讨论】:

    标签: python html beautifulsoup html-parsing


    【解决方案1】:

    这里有一个关于如何使用BeautifulSoup 替换字符串的小示例代码。在您的情况下,您需要一个初步步骤,获取语言之间的映射,可能是字典。

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(html, 'lxml') # or use any other parser
    
    new_string = 'xxx' # replace each string with the same value
    _ = [s.replace_with(new_string) for s in soup.find_all(string=True)]
    
    print(soup.prettify())
    

    【讨论】:

      【解决方案2】:

      您基本上可以这样做来迭代每个元素:

      html="""
      <h1>Heading 1</h1>
      <h2>Heading 2</h2>
      <h3>Heading 3</h3>
      <p>Normal text</p>
      <p><strong>Bold text</strong></p>
      <p><em>Italic text </em></p>
      <p><br></p>
      <blockquote>Quote</blockquote>
      <p>text after quote</p>
      <p><br></p>
      <p><br></p>
      <pre class="code-syntax" spellcheck="false">code</pre>
      <p><br></p>
      <p>text after code</p>
      <p><br></p>
      <p><a href="https://google.com/" target="_blank">This is a search engine</a></p>
      <p><br></p>
      <p><img src="https://via.placeholder.com/350x150"></p>
      """
      
      from bs4 import BeautifulSoup
      
      soup=BeautifulSoup(html,"lxml")
      for x in soup.findAll():
          print(x.text)
          # You can try this as well
          print(x.find(text=True,recursive=False))
          # I think this will return result as you expect.
      

      输出:

      Heading 1
      Heading 2
      Heading 3
      Normal text
      Bold text
      Italic text 
      
      Quote
      text after quote
      
      
      code
      
      text after code
      
      This is a search engine
      
      
      
      Heading 1
      Heading 2
      Heading 3
      Normal text
      Bold text
      Italic text 
      
      Quote
      text after quote
      
      
      code
      
      text after code
      
      This is a search engine
      
      
      
      Heading 1
      Heading 2
      Heading 3
      Normal text
      Bold text
      Bold text
      Italic text 
      Italic text 
      
      
      Quote
      text after quote
      
      
      
      
      code
      
      
      text after code
      
      
      This is a search engine
      This is a search engine
      
      
      
      

      我相信你有翻译功能,你也知道如何替换它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 2016-05-29
        • 2019-09-19
        • 2010-10-24
        • 1970-01-01
        • 2014-06-19
        相关资源
        最近更新 更多