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

BeautifulSoup 如何处理这种情况?

  •  
  •   coolair · 2014-08-08 17:23:45 +08:00 · 2410 次点击
    这是一个创建于 3548 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <div class="a">aaa1</div>
    <div class="b">bbb1</div>

    <div class="a">aaa2</div>

    <div class="a">aaa3</div>
    <div class="b">bbb3</div>

    <div class="a">aaa4</div>

    <div class="a">aaa5</div>
    <div class="b">bbb5</div>

    <div class="a">aaa6</div>
    抓出如下输出:
    aaa1
    bbb1
    ===
    aaa2
    ===
    aaa3
    bbb3
    ===
    aaa4
    ===
    aaa5
    bbb5
    ===
    aaa6

    谢谢!
    2 条回复    2014-08-08 23:54:41 +08:00
    xunyu
        1
    xunyu  
       2014-08-08 18:01:47 +08:00
    findAll{'div'}
    yiding
        2
    yiding  
       2014-08-08 23:54:41 +08:00
    如同楼上所说,然后用 string or contents or get_text() 都可以
    这些在 bs4 的中文文档里面写的很清楚
    http://beautifulsoup.readthedocs.org/zh_CN/latest/

    只不过你的意思貌似还和分组排序有关系,这可能就要和父节点联系起来才有可能得到你想要的分组顺序
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1418 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 17:32 · PVG 01:32 · LAX 10:32 · JFK 13:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.