BeautifulSoup 剔除 HTML script 脚本;删除指定 class标签


BeautifulSoup 剔除 HTML script 脚本,删除指定 class标签

剔除 script

方式一:

[s.extract() for s in soup("script")]

方式二:

    def H5_filter(self):
        '''
        对爬取的 H5 进行过滤
        :return:
        '''
        page = self.crawl_succ_page()
        soup = BeautifulSoup(page, 'lxml')
		
        # 获取文本消息
        title = soup.select('.rich_media_title')[0].get_text()

        tags = soup.find_all()
        for tag in tags:
            if tag.name == 'script':
                tag.decompose()  # 剔除所有 script 脚本
        filter_script_body = soup.find('body')  # 只拿 body
        
        article = soup.find('body').text

        return filter_script_body, article, title

删除指定 class

for span in soup.find_all('span', {'class': 'weapp_display_element js_weapp_display_element'}): # 剔除指定 class
    span.decompose()

如果要删除带有特定id的div,例如decompose(),则可以使用

soup.find('div', id="main-content").decompose()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 使用BeautifulSoup删除html中的script、注释 利用BeautifulSoup去除HTML指定标签和去除注释 html解析和渲染过程 与 Script标签和脚本执行顺序 HTML 脚本 (Script) 实例 BeautifulSoup根据class的属性查找标签的方法 beautifulsoup 根据class属性查找标签的方法 beautifulsoup 根据class属性查找标签的方法。 关于在HTML中使用的script标签 BeautifulSoup获取指定class样式的div HTML中的