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

请教一个 BeautifulSoup.contents 的问题

  •  
  •   saximi · 2017-09-11 19:48:49 +08:00 · 1509 次点击
    这是一个创建于 2424 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> 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 条回复    2017-09-11 23:02:25 +08:00
    ila
        1
    ila  
       2017-09-11 20:53:10 +08:00 via Android
    最后结果都是 list,第一个长度为 2,第二个长度为 1。这是列表的,不是 bs4 的。
    aaronzjw
        2
    aaronzjw  
       2017-09-11 20:58:41 +08:00
    第一行应该写成==> soup=BeautifulSoup("Foo", 'lxml') 吧
    saximi
        3
    saximi  
    OP
       2017-09-11 23:02:25 +08:00
    @aaronzjw 是 BeautifulSoup("Foo", 'lxml'),我漏写了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2216 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.