1

I'm trying Beautiful Soup and using the below code to extract some piece of data.

response = requests.get(url_fii, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text,'html.parser')

print (soup)

I'm getting the below output:

<table class="holiday_list" width="100%">
<tr>
<th colspan="5" style="text-align:center">Heading line</th>
</tr>
<tr>
<th style="text-align:center">Category</th>
<th style="text-align:center">Date</th>
<th style="text-align:center">Value 1</th>
<th style="text-align:center">Value 2</th>
<th style="text-align:center">Value 3</th>
</tr>
<tr class="alt">
<td class="first"> Quantity</td>
<td class="date">09-Apr-2020</td>
<td class="number">7277.03</td>
<td class="number">5539.41</td>
<td class="number">1737.62</td>
</tr>
</table>

Now, the data of my interest is enclosed by < tr >:

By the below code, I'm able to get everything I want:

for p in soup('tr'):
    print (p.text)

Ouput:

Heading line


Category
Date
Value 1
Value 2
Value 3


 Quantity
09-Apr-2020
7277.03
5539.41
1737.62

The only unwanted part is 'Heading line'. Since this is also enclosed in < tr > therefore it is also coming in the output. However, I notice that it has an extra attribute i.e. 'colspan'. How can I use it as a filter so that 'Heading line' doesn't show in the output.

Vishal Sharma
  • 1,670
  • 20
  • 55

2 Answers2

3

You could skip the first element of your array using a slice notation like so:

for p in soup('tr')[1:]:
    print(p.text)

Please, see this post for more information about the slice notation.

Junitar
  • 905
  • 6
  • 13
0

Based on the answer above, here's the code that I'm using now:

response = requests.get(url_fii, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text,'lxml')

for p in soup('tr')[1:]:
    binNames = p.find_all('th')
    binValues = p.find_all('td')
    nBins = 0
    nValues = 0

    #The below section is for calculating the size of binNames. I didn't know of a better way than this.    
    for i in binNames:
            if len(i) > 0:
                nBins += 1

    #Now we print the binNames
    if nBins > 0:
        for i in range(nBins):
            print(binNames[i].text)

    #The below section is for calculating the size of binValues. I didn't know of a better way than this. 
    for i in binValues:
            if len(i) > 0:
                nValues += 1

    #Now we print the binValues
    if nValues > 0:
        for i in range(nValues):
            print(binValues[i].text)
Vishal Sharma
  • 1,670
  • 20
  • 55