BeautifulSoup 怎样才可以 find_all 再次 find_all 结果 - V2EX
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

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

  •  
  •   uyhsefwa Oct 25, 2016 5451 views
    This topic created in 3481 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     3446 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 47ms UTC 10:57 PVG 18:57 LAX 03:57 JFK 06:57
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86