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

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

  •  
  •   gulangyu · 2017-07-15 16:23:48 +08:00 · 7425 次点击
    这是一个创建于 2499 天前的主题,其中的信息可能已经有所发展或是发生改变。

    <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 条回复    2017-07-15 16:57:07 +08:00
    jugelizi
        1
    jugelizi  
       2017-07-15 16:29:14 +08:00
    [0]
    avichen
        2
    avichen  
       2017-07-15 16:35:57 +08:00
    soup.find('p', ,{'class': 'temp temp-low'})
    avichen
        3
    avichen  
       2017-07-15 16:38:25 +08:00
    soup.find('p', {'class': 'temp temp-low'})
    gulangyu
        4
    gulangyu  
    OP
       2017-07-15 16:47:56 +08:00
    @avichen 我想用 select,该怎么做呢?

    temps_tag = seven_day.select('.temp')
    avichen
        5
    avichen  
       2017-07-15 16:52:32 +08:00
    我一般都用 find,你可以尝试通过属性的值来查找
    例如:soup.select('p[class="temp temp-low"]')
    你试一下是不是可以,我不确定是否可行
    gulangyu
        6
    gulangyu  
    OP
       2017-07-15 16:57:04 +08:00
    @avichen 哦,这样似乎不可以。select 就是一层层往下选的。例如: seven_day.select('.tombstone-container img')
    free9fw
        7
    free9fw  
       2017-07-15 16:57:07 +08:00
    pyquery 就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2243 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 07:17 · PVG 15:17 · LAX 00:17 · JFK 03:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.