【问题标题】:Extract all values from div but individually从 div 中提取所有值,但单独提取
【发布时间】:2022-01-26 14:05:32
【问题描述】:
import requests
from bs4 import BeautifulSoup, element


Stock_Symbol=input("Enter the symbol of the stock\n")
Stock_URL="https://ticker.finology.in/company/"+Stock_Symbol
response=requests.get(Stock_URL)
soup=BeautifulSoup(response.text, 'html.parser')
Current_Price = soup.find("div", {"id":"mainContent_clsprice"}).find("span", {"class": "Number"}).getText()
CP=float(Current_Price)
print("Current Price of\t"+Stock_Symbol+"\tis\n",CP)
Market_Cap= soup.find("div", {"id": "mainContent_updAddRatios"}).find("span",{"class":"Number"}).getText()
MC=float(Market_Cap)
print("Market Cap of\t"+Stock_Symbol+"\tin Crores is\n",MC)
No_of_Shares=MC/CP
print("No of shares of\t"+Stock_Symbol+"\tin Crores is\n",No_of_Shares)
EPS= soup.find("div", {"id":"mainContent_updAddRatios"}).find("span",{"class":"Number"}).getText()
print(EPS)

在上面的代码中,<div>id="mainContent_updAddRatios" 包含一些我想单独提取的值,例如 EPSEarningsDebt 等等

我无法找到这样做的方法 - 我使用的符号是 SBIN

【问题讨论】:

  • 好答案需要好问题,请通过详细信息和预期输出改进您的问题,帮助所有人理解您的问题。谢谢
  • 您的问题需要包括输入、预期输出和当前输出。我们不知道您的程序发生了什么,无法判断应该发生什么。

标签: python pandas web-scraping beautifulsoup python-requests


【解决方案1】:

注意: 问题和预期的输出不是很清楚,所以答案只是指向一个方向。

有几种方法可以选择更具体的值/元素 - 一种选择是使用选择器 :-soup-contains() 通过指标名称来选择它

EPS= soup.select_one('#mainContent_updAddRatios div:-soup-contains("EPS (TTM)") .Number').text

Market_Cap= soup.select_one('#mainContent_updAddRatios div:-soup-contains("Market Cap") .Number').text

替代方案可能是:

for i in soup.select('#mainContent_updAddRatios div:has(small)'):
    name = i.small.get_text(strip=True)
    metric = metric.text.strip() if (metric := i.select_one('.Number')) else i.p.text.strip()
    print(f'{name} of {Stock_Symbol} Crores is {metric}')

Output -->

Market Cap of SBIN Crores is 408791.83
CASA % of SBIN Crores is 45.4
No. of Shares of SBIN Crores is 892.46
P/E of SBIN Crores is 15.86
P/B of SBIN Crores is 1.67
Face Value of SBIN Crores is ₹ 1
Div. Yield of SBIN Crores is 0.87 %
Book Value (TTM) of SBIN Crores is 274.32
Net Interest Income of SBIN Crores is 110710
Cost To Income % of SBIN Crores is 53.6
Promoter Holding of SBIN Crores is 57.62 %
EPS (TTM) of SBIN Crores is 28.88
CAR % of SBIN Crores is 13.74
ROE of SBIN Crores is 9.31
ROCE of SBIN Crores is 6.49
Profit Growth of SBIN Crores is 40.88

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多