Valek
Valek

Reputation: 115

Get first td value

I'm new to Beautifulsoup and Python, trying to figure how to get the first th tag of an HTML page. Can someone tell me what is wrong with my code?

HTML

<th width="10%">1365 m</th>
<th width="15%">Rating 25-0</th>
<th width="10%">12h45</th>

I would like to retrieve only the first width which has value 1365. Below is my code

print('Track '+soup.findAll('th',{'width':'10%'})[3])

I tried find('th',{'width':'10%'})[3]) but it is throwing indexbound exception. Any help? With my code i'm getting the second th tag which is 12h45

Upvotes: 0

Views: 815

Answers (2)

innicoder
innicoder

Reputation: 2688

print(soup.findAll('th')[0])

This is the first one.

Computers start counting from 0,1,2,3....n. if you want to print the last one

print(soup.findAll('th')[1])

Why

soup.findAll('th',{'width':'10%'})[3] doesn't work.

We're looking for ALL th's with the width of 10%

In this HTML there's only two.

      <th width="10%">1365 m</th>
      <th width="15%">Rating 25-0</th>
      <th width="10%">12h45</th> 

Best way is to print this:

for i in soup.findAll('th',{'width':'10%'}):
    print(i)

Upvotes: 1

TheoretiCAL
TheoretiCAL

Reputation: 20571

soup.findAll('th',{'width':'10%'})[3] should be:

# Get the all matching 'th' that also has 'width' set to  '10%', access the first match
print('Track '+soup.findAll('th',{'width':'10%'})[0]) 

or if you just want to access the first match:

# Get the first 'th' with 'width' '10%'
soup.find('th',{'width':'10%'})

Upvotes: 0

Related Questions