Python 从BeautifulSoup中的跨度类提取数据/价格

Python 从BeautifulSoup中的跨度类提取数据/价格,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,当我运行此代码时: (BeautifulSoup((requests.get('https://www.theglobeandmail.com/investing/markets/stocks/ENB-T/').text), 'html.parser')).find_all('span', {'class':'barchart-overview-field-value'})[0] 我得到: 如何从上面的span类中提取价格39.21 如有任何建议,我将不胜感激 import requests

当我运行此代码时:

(BeautifulSoup((requests.get('https://www.theglobeandmail.com/investing/markets/stocks/ENB-T/').text), 'html.parser')).find_all('span', {'class':'barchart-overview-field-value'})[0]
我得到:

如何从上面的span类中提取价格39.21

如有任何建议,我将不胜感激

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.theglobeandmail.com/investing/markets/stocks/ENB-T/')
soup = BeautifulSoup(r.content, 'html.parser')
span = soup.find('span', {'class': 'barchart-overview-field-value'})
barchart_field = span.find("barchart-field")
print(barchart_field["value"])
结果

39.21

信息:

只需将这些行添加到您的代码中:

barchart = span.find('barchart-field')

value = barchart['value']

print(value)
输出:

39.21
. 将文本复制并粘贴到问题中,并使用“代码格式”工具正确设置其格式。图像是不可搜索的,屏幕阅读器无法为有视觉障碍的人解读图像。使用链接修改您的问题。