推荐学习书目
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 · 2012 views
    This topic created in 3166 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
        1
    ila  
       Sep 11, 2017 via Android
    最后结果都是 list,第一个长度为 2,第二个长度为 1。这是列表的,不是 bs4 的。
    aaronzjw
        2
    aaronzjw  
       Sep 11, 2017
    第一行应该写成==> soup=BeautifulSoup("Foo", 'lxml') 吧
    saximi
        3
    saximi  
    OP
       Sep 11, 2017
    @aaronzjw 是 BeautifulSoup("Foo", 'lxml'),我漏写了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3237 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:06 · PVG 22:06 · LAX 07:06 · JFK 10:06
    ♥ Do have faith in what you're doing.