zoukankan      html  css  js  c++  java
  • python BeautifulSoup基本用法

    #coding:utf-8
    import os
    from bs4 import BeautifulSoup
    #jsp 路径
    folderPath = "E:/whm/google/src_jsp"
    
    for dirPath,dirNames,fileNames in os.walk(folderPath):
        for fileName in fileNames:
            if fileName.endswith(".jsp"):
                soup=BeautifulSoup(open(os.path.join(dirPath,fileName)),"html.parser")
                if(soup.header is not None):
                    soup.header.extract()
                #属性选择器。。。只能选择出第一个符合规则的元素
                if(soup.find(attrs={'role':'banner'}) is not None):
                    soup.find(attrs={'role':'banner'}).extract()
                if(soup.find(attrs={'class':"col-xs-3"}) is not None):
                    soup.find(attrs={'class':"col-xs-3"}).extract()
                with open(os.path.join(dirPath,fileName),"w+") as file:
                    #pretify()方法返回一个美化过的html 字符串 encode('utf-8')指定编码--
                    file.write(soup.prettify(formatter=None).encode('utf-8'))

    处理jsp页面会出现bug。。。 所以。。不要使用BeautifulSoup处理 jsp和php等脚本页面。。。需要用正则来写。。。这是我摸索半天得来的结论。。。。。

  • 相关阅读:
    word上怎么打钩
    POI操作excel常用方法总结
    web.xml中Filter过滤器标签说明
    IDEA使用操作说明(自己总结)
    windows 64位上oracle 11g安装
    List<T>与List<?>的区别
    java分页之假分页
    CDN之Web Cache
    HTTP之缓存技术
    HTTP之Cookie和Session
  • 原文地址:https://www.cnblogs.com/whm-blog/p/7121895.html
Copyright © 2011-2022 走看看