NRVA
NRVA

Reputation: 507

How can I parse a selected table from webpage with Python BeautifulSoup

I wish to parse the results table from a local sport event (the page basically just contain a table), but when I try with the script below I just get the "menu", not the actual result list. What am I missing?

from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd

site = "https://rittresultater.no/nb/sb_tid/923?pv2=11027&pv1=U"
html = urlopen(site)
soup = BeautifulSoup(html, "lxml") #BeautifulSoup(urlopen(html, "lxml"))

table = soup.select("table")
df = pd.read_html(str(table))[0]
print.df

Upvotes: 0

Views: 43

Answers (1)

wpercy
wpercy

Reputation: 10090

This is happening because there are two <table>s on that page. You can either query on the class name of the table you want (in this case .table-condensed) using the class_ parameter of the find() function, or you can just grab the second table in the list of all tables using the find_all() function.

Solution 1:

table = soup.find('table', class_='table-condensed')
print(table)

Solution 2:

tables = soup.find_all('table')
print(tables[1])

Upvotes: 1

Related Questions