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

菜鸡求教, BeautifulSoup 处理百度百科数据问题

  •  
  •   aeron · 2022-03-09 23:33:58 +08:00 · 2405 次点击
    这是一个创建于 796 天前的主题,其中的信息可能已经有所发展或是发生改变。

    怎么在处理百度百科数据的同时,保留文本内容的章节关系。百科的数据好像都是平铺并列的,没有层级结构,怎么处理才能输出包含层级关系的数据。

    2 条回复    2022-03-10 12:40:05 +08:00
    jeeyong
        1
    jeeyong  
       2022-03-10 02:59:03 +08:00
    我粗看了一下...不能保证可以应用到所有百度百科内容上...
    div[@data-pid=n]
    遍历到没有元素为止, 遍历的时候检测是否有 data-index 属性.
    data-index 是段落标题
    其他就是文本内容...

    可以满足你的需求吗?
    aeron
        2
    aeron  
    OP
       2022-03-10 12:40:05 +08:00 via iPhone
    @jeeyong 非常感谢,不好貌似不行,我试试其他方法吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5201 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 09:47 · PVG 17:47 · LAX 02:47 · JFK 05:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.