Beautifulsoup获得数据价值

时间:2018-09-27 09:09:17

标签: python beautifulsoup

我尝试用BeautifulSoup来捕捉data-value=2538。结果应该是2538。

from urllib.request import urlopen
from bs4 import BeautifulSoup

u = '<span class="mainValueAmount" data-peak="19980" data-timestamp="2018-09-27T10:00:00" data-value="2538">-</span>, <span class="mainValueAmount simpleTextFit" id="ctl00_ContentPlaceHolder1_PublicPagePlaceholder1_PageUserControl_ctl00_PublicPageLoadFixPage_energyYieldWidget_energyYieldValue">2739</span>, <span class="mainValueAmount simpleTextFit" id="ctl00_ContentPlaceHolder1_PublicPagePlaceholder1_PageUserControl_ctl00_PublicPageLoadFixPage_carbonWidget_carbonReductionValue">1.9</span>'

soup = BeautifulSoup(u, "html.parser")

s = soup.select_one('.mainValueAmount')

print(s)

那么,如何捕捉2538值?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用属性:

soup.select_one("span").attrs.get("data-value", None)