Skip to main content
 首页 » 编程设计

python之使用 beautifulsoup 替换 css 类

2024年12月31日7jyk

我正在解析一个 html 文档并想要检索类“FlatParagraph”的所有 css blockquote 标签,但是,有些类包含另一个我想排除的类“FlatParagraph view-history-note”。我的想法是用“view-history-note”类替换“FlatParagraph view-history-note”类。

我的问题与这个类似(如果最好的方法是替换不需要的类)(Replacing class name BeautifulSoup),但是,我需要更新的类保留在原始代码中,因为我有进一步的查询要做我需要一种 find_all() 方法而不是 find() ,因为要替换的类“FlatParagraph view-history-note”不止一个。我想可能是正则表达式,但在将字符串转换回 html 以进行进一步查询时遇到了问题。

非常感谢任何帮助,谢谢。

这是格式示例。我想排除 <BLOCKQUOTE class="FlatParagraph view-history-note">线。

<!doctype html> 
<html lang="en"> 
    <head> 
        <div>I don't want this tag</div> 
        <p>I have already been extracted</p> 
        <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE> 
            <BLOCKQUOTE class="FlatParagraph view-history-note">I don't want to keep this part</BLOCKQUOTE> 
            <BLOCKQUOTE class="FlatParagraph view-history-note">I don't want to keep this part</BLOCKQUOTE> 
            <BLOCKQUOTE class="FlatParagraph view-history-note">I don't want to keep this part</BLOCKQUOTE> 
        <p>I have already been extracted</p> 
        <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE> 
        <p>I have already been extracted</p> 
        <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE> 
        <p>I have already been extracted</p> 
        <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE> 
            <BLOCKQUOTE class="FlatParagraph view-history-note">I don't want to keep this part</BLOCKQUOTE> 
    </head> 
</html> 

我试过了soup.select(".FlatParagraph")但这会检索 .FlatParagraph view-history-note block 引用也是如此。

我也试过从父级获取所有子级,只返回前 10 个左右的子级(html 超过 1000 行)。

上面 html 的预期结果是

[<BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE>, <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE>, <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE>, <BLOCKQUOTE class="FlatParagraph">I want to keep this part</BLOCKQUOTE>] 

请您参考如下方法:

我能想到的两个选项:

# using CSS selector 
r = soup.select('blockquote[class="FlatParagraph"]') 
 
# using lambda 
r = soup.find_all(lambda tag: tag.name == 'blockquote' and tag.get('class') == ['FlatParagraph']) 

(取自BeautifulSoup - How to find a specific class name alone )

两者都将排除没有不需要的标签的结果:

[<blockquote class="FlatParagraph">I want to keep this part</blockquote>, <blockquote class="FlatParagraph">I want to keep this part</blockquote>, <blockquote class="FlatParagraph">I want to keep this part</blockquote>, <blockquote class="FlatParagraph">I want to keep this part</blockquote>] 

但是结果将包含小写的标签,因为 all tags are lowercased as per the HTML specification所以“如果你想保留大小写混合或大写的标签和属性,你需要 parse the document as XML 。”