I am working on a web scrapping project and I need to extract the value of P/E from the given html code through a website. This has to be dynamic in nature. The ticker I have used is IDEA and I am also enclosing the code I have used. I also want to extract values of sales growth for 1 year, 3 year and 5 year from the HTML code next to the below html code.
This on is for PE ratio
<div >
<small>P/E<span data-tooltip="tooltip" title="" data-original-title="It is a valuation parameter that measures the company's current share price relative to its per-share earnings. Generally, high P/E is Overvalued & low P/E is Undervalued."><svg 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 ></i> --></span></small>
<p>
45.16
</p>
</div>
This is for the sales growth
<div >
<div ><span >1 Year</span><span >-27.09%</span></div><div ><span >3 Year</span><span >-5.38%</span></div><div ><span >5 Year</span><span >1.05%</span></div>
</div>
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()
CodePudding user response:
You could try something like this:
import requests
from bs4 import BeautifulSoup
with requests.Session() as session:
for ticker in ['IDEA', 'TIMEXWATCH', 'OFSS']:
(r := session.get(f'https://ticker.finology.in/company/{ticker}')).raise_for_status()
soup = BeautifulSoup(r.content, 'lxml')
pe = soup.find(id='mainContent_updAddRatios').find_all('div')[3].find('p').text.strip()
print(f'P/E for {ticker} = {pe}')