Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the nth element using BeautifulSoup

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?

like image 646
aadvaark Avatar asked Jan 04 '12 09:01

aadvaark


People also ask

How do I find a specific element with BeautifulSoup?

BeautifulSoup has a limited support for CSS selectors, but covers most commonly used ones. Use select() method to find multiple elements and select_one() to find a single element.

What is Find () method in BeautifulSoup?

find() method The find method is used for finding out the first tag with the specified name or id and returning an object of type bs4. Example: For instance, consider this simple HTML webpage having different paragraph tags.


2 Answers

You could also use findAll to get all the rows in a list and after that just use the slice syntax to access the elements that you need:

rows = soup.findAll('tr')[4::5] 
like image 104
jcollado Avatar answered Oct 04 '22 16:10

jcollado


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 
like image 37
Sony Mathew Avatar answered Oct 04 '22 14:10

Sony Mathew