【问题标题】:Having issue while printing specific value from df从 df 打印特定值时出现问题
【发布时间】:2022-01-25 22:36:54
【问题描述】:
import pandas as pd
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')
data_array = soup.find_all(id="mainContent_clsprice")
df=pd.DataFrame(data_array)
a=(df[1])
print(a)

在上面的代码中,我只想提取今天的价格是 456,但是我得到的值是 [\n, [456.95], [], \n],有什么方法可以得到网页抓取时的特定值

【问题讨论】:

  • 你输入什么符号?在你使用input 之后?这样我们就可以精确复制。
  • 我用SBIN作为符号

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


【解决方案1】:

您不需要 pandas 来获取这些数据! 这是一个工作示例:

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')
data = soup.find("div", {"id":"mainContent_clsprice"}).find("span", {"class": "Number"}).getText()

print(data)

作为输出

Enter the symbol of the stock
itc
218.00

【讨论】:

  • 非常感谢,工作顺利。
猜你喜欢
  • 1970-01-01
  • 2020-03-15
  • 2016-01-03
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2021-01-22
相关资源
最近更新 更多