V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
uyhsefwa
V2EX  ›  Python

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

  •  
  •   uyhsefwa · Oct 25, 2016 · 5446 views
    This topic created in 3478 days ago, the information mentioned may be changed or developed.
    就是我之前用 find_all 定位了一个标签,然后我想再次用 find_all 查询第一次 find_all 的结果,可是却失败了
    14 replies    2016-10-25 12:49:07 +08:00
    uyhsefwa
        1
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    或者可不可以把 find_all 的结果全部转换成 string ,
    b1eberg0n
        2
    b1eberg0n  
       Oct 25, 2016
    a = soup.findall('a')
    b = a[0].findall('b')
    Arthur2e5
        3
    Arthur2e5  
       Oct 25, 2016
    改写一下 @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')]
    uyhsefwa
        4
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    uyhsefwa
        5
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    @b1eberg0n @Arthur2e5 我把报错图片上传了,您看一下
    orange88
        6
    orange88  
       Oct 25, 2016 via Android
    li 是 none ,上一个没找到?
    uyhsefwa
        7
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    @orange88 li[0]能打印出来,我用 print 打印出了,但是加上 b=li 那句话就打印失败了,很费解
    orange88
        8
    orange88  
       Oct 25, 2016 via Android
    li[0].findall
    uyhsefwa
        9
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    @orange88 好的,谢谢,等下上完课回去试试
    syahd
        10
    syahd  
       Oct 25, 2016 via Android
    find_all 返回的结果是个 list ,要其中的结果得把它迭代一遍才行,最好再加个异常处理,防止出现你这样的情况
    practicer
        11
    practicer  
       Oct 25, 2016
    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  
       Oct 25, 2016
    转成 tag 再 findall
    b1eberg0n
        13
    b1eberg0n  
       Oct 25, 2016
    @771456556

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

    再试试
    uyhsefwa
        14
    uyhsefwa  
    OP
       Oct 25, 2016 via Android
    @b1eberg0n 改成 find_all 就好了…
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2563 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 04:13 · PVG 12:13 · LAX 21:13 · JFK 00:13
    ♥ Do have faith in what you're doing.