推荐学习书目
› 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
jayan2358
V2EX  ›  Python

如何清理爬虫数据中一些不需要的 HTML 属性

  •  
  •   jayan2358 · Apr 8, 2018 · 5508 views
    This topic created in 3135 days ago, the information mentioned may be changed or developed.

    比如对于以下数据

    <p id="a">data</p>
    

    我只想保留

    <p>data</p>
    

    该如何操作,有快捷的方法吗?

    10 replies  •  2018-04-09 10:22:56 +08:00
    xgzxy
    xgzxy
    Apr 8, 2018
    1
    用 text()提取出文本吧,就能取出 data 了吧
    sheep3
    sheep3
    Apr 8, 2018❤️1
    2
    直接正则 replaceAll("<\S+\s(.*?)>","")
    唔..... 大概有这种语法吧.....
    Daniel65536
    Daniel65536
    Apr 8, 2018 via iPhone❤️2
    3
    lxml.html.clean
    hanxiV2EX
    hanxiV2EX
    Apr 8, 2018 via Android
    4
    二楼的正则可以搞定吧
    woscaizi
    woscaizi
    Apr 8, 2018 via iPhone
    5
    Xpath 语法可解
    jayan2358
    jayan2358
    OP
    Apr 8, 2018
    7
    @woscaizi
    /text() 吗,我还是要保留 html 标签的,毕竟内容里有 img
    Luckyray
    Luckyray
    Apr 8, 2018
    8
    为什么要保留 html 文件?保存下来想要的数据不行吗
    qsnow6
    qsnow6
    Apr 8, 2018
    9
    都让开,看我这个贴: https://www.v2ex.com/t/342064#reply12
    soulmine
    soulmine
    Apr 9, 2018
    10
    @jayan2358 你要 img 直接 attr 取嘛 和你 text 不矛盾啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1497 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 01:21 · PVG 09:21 · LAX 17:21 · JFK 20:21
    ♥ Do have faith in what you're doing.