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

Beautifulsoups 有两个值的 class 标签该如何 select?

  •  
  •   gulangyu · Jul 15, 2017 · 8247 views
    This topic created in 3399 days ago, the information mentioned may be changed or developed.

    <p class="temp">文本内容</p>

    如果 class 里面只有一个值,就用 soup.select('.temp')

    <p class="temp temp-low">文本内容</p>

    如果有两个的话,不管我写成 soup.select('.temp.temp-low') 还是 soup.select('.temp .temp-low') 结果都不是预期想要的。

    7 replies  •  2017-07-15 16:57:07 +08:00
    jugelizi
    jugelizi
    Jul 15, 2017
    1
    [0]
    avichen
    avichen
    Jul 15, 2017
    2
    soup.find('p', ,{'class': 'temp temp-low'})
    avichen
    avichen
    Jul 15, 2017
    3
    soup.find('p', {'class': 'temp temp-low'})
    gulangyu
    gulangyu
    OP
    Jul 15, 2017
    4
    @avichen 我想用 select,该怎么做呢?

    temps_tag = seven_day.select('.temp')
    avichen
    avichen
    Jul 15, 2017
    5
    我一般都用 find,你可以尝试通过属性的值来查找
    例如:soup.select('p[class="temp temp-low"]')
    你试一下是不是可以,我不确定是否可行
    gulangyu
    gulangyu
    OP
    Jul 15, 2017
    6
    @avichen 哦,这样似乎不可以。select 就是一层层往下选的。例如: seven_day.select('.tombstone-container img')
    free9fw
    free9fw
    Jul 15, 2017
    7
    pyquery 就好了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2002 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 16:32 · PVG 00:32 · LAX 08:32 · JFK 11:32
    ♥ Do have faith in what you're doing.