V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-03-27 19:16:59 +08:00 · 12984 次点击
    这是一个创建于 2973 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2016-03-28 13:32:35 +08:00
    dsp2138
        1
    dsp2138  
    OP
       2016-03-27 19:36:17 +08:00

    没人给解答下吗
    qiu0130
        2
    qiu0130  
       2016-03-27 21:31:17 +08:00
    dsp2138
        3
    dsp2138  
    OP
       2016-03-27 22:36:48 +08:00
    @qiu0130 谢谢, github 我这里访问受限!
    我先尝试下.stripped_strings
    xj90512
        4
    xj90512  
       2016-03-28 10:49:42 +08:00
    .replace
    dsp2138
        5
    dsp2138  
    OP
       2016-03-28 13:32:35 +08:00
    @xj90512 单就去除空格之类的字符应该可以,但如想保留 html 标签,该怎么做?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2206 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:48 · PVG 12:48 · LAX 21:48 · JFK 00:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.