Skip to content Skip to sidebar Skip to footer

Scrape Table With Beautifulsoup

I have a table structure that looks like this : testtestestes

Solution 1:

If you want to pair them up, create an iterator from soup.find_all('tr') and zip them into pairs:

it = iter(soup.find_all('tr'))
for tr1, tr2  inzip(it, it):
        tds = tr1.find_all('td') + tr2.find_all("td")
        print(tds)

The equivalent with slicing would be to start with a different start pos and use a step of 2:

it = soup.find_all('tr')
for tr1, tr2  inzip(it[::2], it[1::2]):
        tds = tr1.find_all('td') + tr2.find_all("td")
        print(tds)

Using iter means you don't need to shallow copy the list.

Not sure how having an uneven amount of trs fits into the logic as there would be nothing to pair but if there is you can use izip_longest:

from itertools import izip_longest # python3 zip_longest

it = iter(soup.find_all('tr'))
for tr1, tr2  in izip_longest(it, it):
        tds = tr1.find_all('td') + tr2.find_all("td") if tr2 else []
        print(tds)

Post a Comment for "Scrape Table With Beautifulsoup"