Getting the nth element using BeautifulSoup

后端 未结 5 1856
生来不讨喜
生来不讨喜 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:41

    This can be easily done with select in beautiful soup if you know the row numbers to be selected. (Note : This is in bs4)

    row = 5
    while true
        element = soup.select('tr:nth-of-type('+ row +')')
        if len(element) > 0:
            # element is your desired row element, do what you want with it 
            row += 5
        else:
            break
    

提交回复
热议问题