有什么方法可以从网页中提取给定 html 代码中 P/E 比率的值

Is there any way to extract the value of P/E ration in the given html code from a web page

我正在做一个网络抓取项目,我需要通过网站从给定的 html 代码中提取 P/E 的值。这在本质上必须是动态的。我使用的代码是 IDEA,我也附上了我使用的代码。我还想从下面 html 代码旁边的 HTML 代码中提取 1 年、3 年和 5 年的销售增长值。

这个是为了市盈率

<div class="col-6 col-md-4 compess">
    <small>P/E<span class="infolink" 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 &amp; low P/E is Undervalued.">
        <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>
    </small>
    <p>
    5.16
    </p>
</div>

这是为了销售增长

<div class="w-100">
    <div class="ratiosingle">
        <span class="duration">1 Year</span>
        <span class="durationvalue">-27.09%</span>
    </div>
    <div class="ratiosingle">
        <span class="duration">3 Year</span>
        <span class="durationvalue">-5.38%</span>
    </div>
    <div class="ratiosingle">
        <span class="duration">5 Year</span>
        <span class="durationvalue">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()

您可以尝试这样的操作:

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}')