V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 5079 views
    This topic created in 2954 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
        1
    xgzxy  
       Apr 8, 2018
    用 text()提取出文本吧,就能取出 data 了吧
    sheep3
        2
    sheep3  
       Apr 8, 2018   ❤️ 1
    直接正则 replaceAll("<\S+\s(.*?)>","")
    唔..... 大概有这种语法吧.....
    Daniel65536
        3
    Daniel65536  
       Apr 8, 2018 via iPhone   ❤️ 2
    lxml.html.clean
    hanxiV2EX
        4
    hanxiV2EX  
       Apr 8, 2018 via Android
    二楼的正则可以搞定吧
    woscaizi
        5
    woscaizi  
       Apr 8, 2018 via iPhone
    Xpath 语法可解
    jayan2358
        7
    jayan2358  
    OP
       Apr 8, 2018
    @woscaizi
    /text() 吗,我还是要保留 html 标签的,毕竟内容里有 img
    Luckyray
        8
    Luckyray  
       Apr 8, 2018
    为什么要保留 html 文件?保存下来想要的数据不行吗
    qsnow6
        9
    qsnow6  
       Apr 8, 2018
    都让开,看我这个贴: https://www.v2ex.com/t/342064#reply12
    soulmine
        10
    soulmine  
       Apr 9, 2018
    @jayan2358 你要 img 直接 attr 取嘛 和你 text 不矛盾啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1087 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 23:23 · PVG 07:23 · LAX 16:23 · JFK 19:23
    ♥ Do have faith in what you're doing.