Getting the nth element using BeautifulSoup

后端 未结 5 1855
生来不讨喜
生来不讨喜 2021-01-31 04:09

From a large table I want to read rows 5, 10, 15, 20 ... using BeautifulSoup. How do I do this? Is findNextSibling and an incrementing counter the way to go?

5条回答
  •  执笔经年
    2021-01-31 04:42

    Another option, if you prefer raw html...

    """Build a small table with one column and ten rows, then parse it into a list"""
    htstring = """
    foo1
    foo2
    foo3
    foo4
    foo5
    foo6
    foo7
    foo8
    foo9
    foo10
    """ result = [html_tr for idx, html_tr in enumerate(soup.findAll('tr')) \ if (idx+1)%5==0] print result

    Running that...

    [mpenning@Bucksnort ~]$ python testme.py
    [ foo5 ,  foo10 ]
    [mpenning@Bucksnort ~]$
    

提交回复
热议问题