【问题标题】:Replacing header contents with empty string in Beautiful Soup在 Beautiful Soup 中用空字符串替换标题内容
【发布时间】:2013-11-03 12:42:57
【问题描述】:

我有一个代码可以删除 head 标签中的文本。给我们一个网站的html

    for link in soup.findAll('head'):
        link.replaceWith("")

我正在尝试用“”替换整个内容。然而,这是行不通的。如何从汤中完全删除头部标签之间的所有文本。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    试试这个:

    [head.extract() for head in soup.findAll('head')]
    

    【讨论】:

      【解决方案2】:

      您需要使用 """(3 个引号),但您似乎只使用了两个。

      例子:

      """
      This block
      is commented out
      """
      

      编码愉快!

      编辑:这不是用户所要求的,我很抱歉。

      我没有使用 Beautiful Soup 的经验,但我在 SO 上找到了一段可能对您有用的代码 (source):

      soup = BeautifulSoup(source.lower())
      to_extract = soup.findAll('ahref') #Edit the stuff inside '' to change which tag you want items to be removed from, like 'ahref' or 'head'
      for item in to_extract:
          item.extract()
      

      不过,从外观上看,它可能只是删除了您页面上的每个链接。

      如果这对您没有更多帮助,我很抱歉!

      【讨论】:

      • 哦!哦,我现在明白了。我正在修改我的答案,我很抱歉。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2016-06-10
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      相关资源
      最近更新 更多