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.