SIM
SIM

Reputation: 22440

How to scrape the data of two tables sitting side by side in a webpage?

I've written a script in python in combination with BeautiflSoup using .select() to parse the tabular data from some html elements. The data in reality are within two tables. When I open that site I can see that the two tables stay side by side like you can see in this image. I marked the second table with a pencil to let you know that it is in reality the second table sitting close to each other.

However, when I execute my below script, I get the output one after another. How can I source them like they are visible in that image, meaning side by side.

I could not paste the html elements as they are too huge to fit to the below area. However, that can be found in this link. Could not share the website link as it required log in to reach the data.

from bs4 import BeautifulSoup

htmldoc = """replace_with_above_elements"""

soup = BeautifulSoup(htmldoc,"lxml")
for items in soup.select("tbody.Table2__tbody tr"):
    data = [item.text for item in items.select("td")]
    print(data)

Btw, the two tables are of similar structures.

Current output:

['PG', 'Empty', '', '--', '']
['SG', 'Eric GordonHouSG', '', '@LAC', '7:00 AM']
['SF', 'Taurean PrinceAtlSF', '', '@Cle', '4:00 AM']
['PF', 'Empty', '', '--', '']
['C', 'Jusuf NurkicPorC', '', '--', '']
['UTIL', 'Paul GeorgeOKCSF', '', 'Sac', '5:00 AM']
['UTIL', 'Gary HarrisDenSG', '', 'GS', '6:00 AM']
['UTIL', "D'Angelo RussellBknPG, SG", '', '--', '']
['Bench', 'Damian LillardPorPG', '', '--', '']
['Bench', 'Devin BookerPhxSG', '', '--', '']
['Bench', 'Deandre AytonPhxC', '', '--', '']
['Bench', 'Mike ConleyMemPG', '', '--', '']
['Bench', 'Trevor ArizaPhxSF', '', '--', '']
['Bench', 'Serge IbakaTorPF', '', '--', '']
['IR', 'Isaiah ThomasODenPG', '', 'GS', '6:00 AM']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--']
['30.5', '5.5/13.5', '.407', '3.0/3.0', '1.000', '1.0', '2.0', '1.0', '0.0', '1.0', '0.5', '15.0', '0.82', '53.1', '+5.0']
['27.5', '8.5/15.0', '.567', '4.0/4.0', '1.000', '3.5', '5.0', '4.5', '1.5', '0.0', '5.0', '24.5', '5.76', '49.2', '+10.8']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--']
['20.0', '6.5/13.0', '.500', '1.0/1.0', '1.000', '0.0', '8.5', '1.0', '1.5', '0.5', '2.0', '14.0', '2.05', '78.6', '-5.1']
['37.5', '8.0/25.0', '.320', '4.0/5.5', '.727', '3.5', '5.0', '4.5', '3.0', '0.0', '4.0', '23.5', '1.80', '99.8', '-0.1']
['34.5', '6.5/15.5', '.419', '5.5/6.0', '.917', '0.5', '3.5', '3.0', '1.0', '0.5', '0.5', '19.0', '3.08', '76.4', '-5.5']
['30.0', '4.3/12.3', '.351', '1.3/1.7', '.800', '1.7', '4.3', '6.0', '0.7', '0.7', '3.0', '11.7', '2.43', '81.2', '-6.3']
['32.5', '9.0/18.0', '.500', '8.0/8.0', '1.000', '2.5', '4.0', '6.5', '0.5', '0.5', '1.5', '28.5', '8.47', '99.9', '0.0']
['34.5', '8.5/17.0', '.500', '9.5/11.5', '.826', '3.5', '3.0', '7.0', '0.5', '0.0', '4.5', '30.0', '4.30', '99.7', '+0.2']
['29.5', '5.0/9.0', '.556', '1.5/2.0', '.750', '0.0', '9.0', '3.5', '0.5', '0.5', '1.0', '11.5', '2.14', '98.4', '-0.2']
['27.0', '4.5/12.0', '.375', '2.5/3.0', '.833', '2.0', '3.0', '7.0', '0.5', '0.0', '0.5', '13.5', '2.09', '92.2', '-2.6']
['33.0', '4.5/9.5', '.474', '1.0/1.0', '1.000', '3.0', '5.5', '4.0', '0.5', '0.5', '1.5', '13.0', '3.31', '40.9', '+31.2']
['29.3', '5.3/11.3', '.471', '4.0/5.7', '.706', '0.7', '7.3', '1.0', '1.0', '1.7', '1.7', '15.3', '5.29', '36.0', '+2.4']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '37.1', '-17.9']

Expected output is visible in the above image link.

Upvotes: 2

Views: 69

Answers (1)

Andersson
Andersson

Reputation: 52665

You can try to handle two tables as below:

soup = BeautifulSoup(htmldoc,"lxml")

tables = soup.select("tbody.Table2__tbody")

for left, right in zip(tables[0].select("tr"), tables[1].select("tr")):
    data = [item.text for item in left.select("td")] + [item.text for item in right.select("td")]
    print(data)

Upvotes: 2

Related Questions