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
gulangyu
V2EX  ›  Python

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

  •  
  •   gulangyu · Jul 15, 2017 · 7851 views
    This topic created in 3214 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
        1
    jugelizi  
       Jul 15, 2017
    [0]
    avichen
        2
    avichen  
       Jul 15, 2017
    soup.find('p', ,{'class': 'temp temp-low'})
    avichen
        3
    avichen  
       Jul 15, 2017
    soup.find('p', {'class': 'temp temp-low'})
    gulangyu
        4
    gulangyu  
    OP
       Jul 15, 2017
    @avichen 我想用 select,该怎么做呢?

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