推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
dsp2138
V2EX  ›  Python

Beautifulsoup 如何过滤出换行、空行以及空格和中文空格

  •  
  •   dsp2138 · Mar 27, 2016 · 13773 views
    This topic created in 3843 days ago, the information mentioned may be changed or developed.
    Beautifulsoup 如何过滤出换行、空行以及空格和中文空格!
    请教,对于 html 中存在很多空行,空格的处理问题!
    很多文章是存 word 中直接复制到到网页上的,对于这种方式编辑的 html 结构可能比较混乱!
    如何净化或者说格式化这种 html !
    快速过滤出换行符、空格、中文空格等

    对于正则已经简单写处理方法,凑合能用

    def del_content_blank(s):
    clean_str = re.sub(r'\n|&nbsp|\xa0|\\xa0|\u3000|\\u3000|\\u0020|\u0020', '', str(s))
    return clean_str

    但 Beautifulsoup 中对于换行是作为一个子节点解析的!那我怎么才能用 Beautifulsoup 模块过滤换行、空行以及空格和中文空格?这个问题请假大家,谢谢
    5 replies  •  2016-03-28 13:32:35 +08:00
    dsp2138
        1
    dsp2138  
    OP
       Mar 27, 2016
    ?
    没人给解答下吗
    dsp2138
        3
    dsp2138  
    OP
       Mar 27, 2016
    @qiu0130 谢谢, github 我这里访问受限!
    我先尝试下.stripped_strings
    xj90512
        4
    xj90512  
       Mar 28, 2016
    .replace
    dsp2138
        5
    dsp2138  
    OP
       Mar 28, 2016
    @xj90512 单就去除空格之类的字符应该可以,但如想保留 html 标签,该怎么做?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2579 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 807ms · UTC 03:27 · PVG 11:27 · LAX 20:27 · JFK 23:27
    ♥ Do have faith in what you're doing.