'I need to web scrap a particular value from a page which is contained in a table

I want to scrap net sales value for Dec 2021 that is contained in a table from a webpage. I am using simple beautifulsoup module.I have included the python code I am using for extracting some of the other values.I want to extract the value 9644.8. The code for the webpage is give below

<table class="table table-sm table-hover screenertable  table-responsive-sm">
    <thead>
    <tr>
        <th scope="col">PARTICULARS</th>
        <th scope="col">Dec 2020</th>
        <th scope="col">Mar 2021</th>
        <th scope="col">Jun 2021</th>
        <th scope="col">Sep 2021</th>
        <th scope="col">Dec 2021</th>
    </tr>
    </thead>
    <tbody>
    <tr class="">
        <th scope="row">Net Sales <span class="infolink" data-tooltip="tooltip" title=""
                                        data-original-title="It is companys core revenue net of discounts and returns."><svg
                class="svg-inline--fa fa-info-circle fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas"
                data-icon="info-circle" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"
                data-fa-i2svg=""><path fill="currentColor"
                                       d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path></svg>
            <!-- <i class="fas fa-info-circle"></i> --></span></th>
        <td>
            <span class="Number" value="10824.4">10,824.40</span>
        </td>
        <td>
            <span class="Number" value="9530.9">9,530.90</span>
        </td>
        <td>
            <span class="Number" value="9088.2">9,088.20</span>
        </td>
        <td>
            <span class="Number" value="9321.5">9,321.50</span>
        </td>
        <td>
            <span class="Number" value="9644.8">9,644.80</span>
        </td>
    </tr>
    </tbody>
</table>
import pandas as pd
from bs4 import BeautifulSoup
import requests
a=input("Enter symbol of the company\n")
url="https://ticker.finology.in/company/"+a
print(url)
response=requests.get(url)
soup=BeautifulSoup(response.text,'html.parser')
CP = soup.find("div", {"id":"mainContent_clsprice"}).find("span", {"class": "Number"}).getText()

The ticker I am using is IDEA



Solution 1:[1]

Provided you have located the correct table you can use select_one and the css selector tbody td:last-child > .Number. tbody will restrict to the table body rows, select_one with td:last-child will grab the first last column, then the > .Number will grab the direct child span. You could of course, combine and shorten the selector list rather than separately grabbing table, however, I was unsure if you would do other things with the table.

from bs4 import BeautifulSoup as bs
import requests

r = requests.get('https://ticker.finology.in/company/idea', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
table = soup.select_one('.card:has(> #mainContent_hfSizeForQuarter) table')
table.select_one('tbody td:last-child > .Number').text.strip()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 QHarr