Jeff Barrette
Jeff Barrette

Reputation: 33

Trying to use BeautifulSoup to find a specific table in an HTML doc

The HTLM page I'm trying to read has 21 tables. The specific table I'm trying to reference is unique in that is has a unique <caption> and not all tables even have a caption.

Here is a snippet of the structure:

<table class="wikitable">
    <caption>Very long caption</caption>
    <tbody>
        <tr align="center" bgcolor="#efefef">

I've tried:

soup = BeautifulSoup(r.text, "html.parser")
table1 = soup.find('table', caption="Very long caption")

But returns a None object.

Upvotes: 3

Views: 1478

Answers (1)

alecxe
alecxe

Reputation: 473873

soup.find('table', caption="Very long caption")

This basically means - locate a table element that has a caption attribute having Very long caption value. This obviously returns nothing.

What I would do is to locate the caption element by text and get the parent table element:

soup.find("caption", text="Very long caption").find_parent("table")

Upvotes: 4

Related Questions