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

请教一个 BeautifulSoup.contents 的问题

  •  
  •   saximi · Sep 11, 2017 · 2385 views
    This topic created in 3342 days ago, the information mentioned may be changed or developed.
    >>> soup = BeautifulSoup("Foo") 
    >>> soup.a.append("Bar") 
    
    >>> soup 
    FooBar 
    >>> soup.a.contents 
    [u'Foo', u'Bar'] 
    
    上面代码执行后,soup.a.contents 列表中有两项内容,但是下面代码执行后,列表中却只有一项内容,为何会有这个差别?感谢指点! 
    
    >>> soup = BeautifulSoup("FooBar")  
    >>> soup 
    FooBar 
    >>> soup.a.contents 
    ['FooBar'] 
    
    
    3 replies  •  2017-09-11 23:02:25 +08:00
    ila
    ila
    Sep 11, 2017 via Android
    1
    最后结果都是 list,第一个长度为 2,第二个长度为 1。这是列表的,不是 bs4 的。
    aaronzjw
    aaronzjw
    Sep 11, 2017
    2
    第一行应该写成==> soup=BeautifulSoup("Foo", 'lxml') 吧
    saximi
    saximi
    OP
    Sep 11, 2017
    3
    @aaronzjw 是 BeautifulSoup("Foo", 'lxml'),我漏写了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3204 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:15 · PVG 18:15 · LAX 02:15 · JFK 05:15
    ♥ Do have faith in what you're doing.