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

我想修改 BS4 的原代码,让其禁止重新排序,不知道该改哪?

  •  
  •   omg21 · Nov 26, 2016 · 2624 views
    This topic created in 3593 days ago, the information mentioned may be changed or developed.
    还是关于这个 BS4 处理代码重新排序的问题,如下:
    <td height="250" class="infodetail" id="TDContent" valign="top">
    上边这行代码经过 BeautifulSoup 处理后会变成
    <td class="infodetail" height="250" id="TDContent" valign="top">
    根据标签属性的顺序进行了重新排序。

    我现在想着手在 BS4 的原代码里修改,让其禁止排序,原样输出,可是看了代码后就蒙了懵了,根本不知道从哪下手啊。。。
    __init__.py dammit.py diagnose.py element.py testing.py
    BS4 有这几个文件,得从哪里开始啃?
    5 replies  •  2016-11-26 20:27:06 +08:00
    sagaxu
        1
    sagaxu  
       Nov 26, 2016
    attrs 是用 dict 存的, dict 本身就不保证顺序,然后在 decode 的时候还做了排序,如果要保序,你需要把 attrs 改造成 list ,然后所有读写 attrs 的地方都要修改,估计得小半天才能改完
    mkeith
        2
    mkeith  
       Nov 26, 2016
    顺序有什么关系吗?
    congeec
        3
    congeec  
       Nov 26, 2016 via iPhone
    启动 python 前加 PYTHONHASHSEED=0 环境变量。搜一下你就知道为吗乱续了
    LaoK666
        4
    LaoK666  
       Nov 26, 2016
    @sagaxu OrderedDict 会不会更容易改些……
    264768502
        5
    264768502  
       Nov 26, 2016 via Android
    乱序是 xml 标准的一环
    不是 bs4 带来的乱序,是 xml 的 parser 引起的乱序
    elementtree 可以 hack 它的函数,来保持原有的顺序(sf 上有人贴了代码的)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   853 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    ♥ Do have faith in what you're doing.