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

BeautifulSoup 怎样才可以 find_all 再次 find_all 结果

  •  
  •   771456556 · 2016-10-25 01:01:34 +08:00 · 4591 次点击
    这是一个创建于 2742 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就是我之前用 find_all 定位了一个标签,然后我想再次用 find_all 查询第一次 find_all 的结果,可是却失败了
    14 条回复    2016-10-25 12:49:07 +08:00
    771456556
        1
    771456556  
    OP
       2016-10-25 01:03:53 +08:00 via Android
    或者可不可以把 find_all 的结果全部转换成 string ,
    b1eberg0n
        2
    b1eberg0n  
       2016-10-25 01:05:56 +08:00
    a = soup.findall('a')
    b = a[0].findall('b')
    Arthur2e5
        3
    Arthur2e5  
       2016-10-25 01:47:59 +08:00
    改写一下 @b1eberg0n ,来个范围攻击的……

    a_b = [a.findall('b') for a in soup.findall('a')]
    a_b_flat = [result for a in soup.findall('a') for result in a.findall('b')]
    771456556
        4
    771456556  
    OP
       2016-10-25 07:28:23 +08:00 via Android
    771456556
        5
    771456556  
    OP
       2016-10-25 07:29:00 +08:00 via Android
    @b1eberg0n @Arthur2e5 我把报错图片上传了,您看一下
    orange88
        6
    orange88  
       2016-10-25 07:34:02 +08:00 via Android
    li 是 none ,上一个没找到?
    771456556
        7
    771456556  
    OP
       2016-10-25 07:37:34 +08:00 via Android
    @orange88 li[0]能打印出来,我用 print 打印出了,但是加上 b=li 那句话就打印失败了,很费解
    orange88
        8
    orange88  
       2016-10-25 07:41:42 +08:00 via Android
    li[0].findall
    771456556
        9
    771456556  
    OP
       2016-10-25 07:44:39 +08:00 via Android
    @orange88 好的,谢谢,等下上完课回去试试
    whwq2012
        10
    whwq2012  
       2016-10-25 08:12:31 +08:00 via Android
    find_all 返回的结果是个 list ,要其中的结果得把它迭代一遍才行,最好再加个异常处理,防止出现你这样的情况
    practicer
        11
    practicer  
       2016-10-25 09:08:16 +08:00
    get_all_secondary_elements(bs)

    try:
    top_elements = bs.find_all(...)

    for top_element in top_elements:

    secondary_elements = top_element.find_all(...)

    if secondary_elements is not None:
    yield secondary_elements

    except (AttributeError, TypeError):
    yield


    secondary_elements = list(get_all_secondary_elements)
    wyntergreg
        12
    wyntergreg  
       2016-10-25 10:10:53 +08:00
    转成 tag 再 findall
    b1eberg0n
        13
    b1eberg0n  
       2016-10-25 10:24:54 +08:00
    @771456556

    li = soup.findAll('div', class = 'div_content')
    print li
    b = li[0].findAll('ul')
    print b

    再试试
    771456556
        14
    771456556  
    OP
       2016-10-25 12:49:07 +08:00 via Android
    @b1eberg0n 改成 find_all 就好了…
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1108 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 18:11 · PVG 02:11 · LAX 11:11 · JFK 14:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.