You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从HTML代码中提取"Owners estimation: "后的无逗号数字?

实现方案:提取并格式化所有者估算数字

这里提供几种不同场景下的实现方法,满足从指定HTML片段中提取目标数字并移除逗号的需求:

方法一:Python + BeautifulSoup(适合后端/数据处理场景)

通过HTML解析库提取文本内容,再分割处理数字:

from bs4 import BeautifulSoup

html = '<p style="font-size: 150%;margin-bottom:10px" data-toggle="tooltip" data-placement="bottom" title="Estimation is the process of finding an estimate or approximation, which is a value that is usable for some purpose even if input data may be incomplete, uncertain, or unstable. The value is nonetheless usable because it is derived from the best information available.">Owners estimation: 4,253,717</p>'

# 解析HTML获取p标签文本
soup = BeautifulSoup(html, 'html.parser')
p_content = soup.find('p').get_text()

# 分割出数字部分并移除逗号
number_segment = p_content.split('Owners estimation: ')[1].strip()
cleaned_number = number_segment.replace(',', '')

print(cleaned_number)  # 输出结果:4253717

方法二:JavaScript(适合前端/浏览器环境)

直接操作DOM或用cheerio在Node.js中处理:

浏览器端实现

// 定位目标p元素
const targetElement = document.querySelector('p[data-toggle="tooltip"]');
const elementText = targetElement.textContent;

// 提取数字并移除逗号
const numberPart = elementText.split('Owners estimation: ')[1].trim();
const finalNumber = numberPart.replace(/,/g, '');

console.log(finalNumber); // 输出结果:4253717

Node.js环境(使用cheerio)

const cheerio = require('cheerio');
const html = '<p style="font-size: 150%;margin-bottom:10px" data-toggle="tooltip" data-placement="bottom" title="Estimation is the process of finding an estimate or approximation, which is a value that is usable for some purpose even if input data may be incomplete, uncertain, or unstable. The value is nonetheless usable because it is derived from the best information available.">Owners estimation: 4,253,717</p>';

const $ = cheerio.load(html);
const pText = $('p').text();
const numberSegment = pText.split('Owners estimation: ')[1].trim();
const cleanedNumber = numberSegment.replace(/,/g, '');

console.log(cleanedNumber); // 输出结果:4253717

方法三:纯正则表达式(通用跨语言方案)

无需HTML解析库,直接用正则匹配目标数字片段:

# Python示例
import re

html = '<p style="font-size: 150%;margin-bottom:10px" data-toggle="tooltip" data-placement="bottom" title="Estimation is the process of finding an estimate or approximation, which is a value that is usable for some purpose even if input data may be incomplete, uncertain, or unstable. The value is nonetheless usable because it is derived from the best information available.">Owners estimation: 4,253,717</p>'

match_result = re.search(r'Owners estimation: ([\d,]+)', html)
if match_result:
    number_with_commas = match_result.group(1)
    cleaned_number = number_with_commas.replace(',', '')
    print(cleaned_number)  # 输出结果:4253717

正则表达式说明:Owners estimation: ([\d,]+) 会精准匹配"Owners estimation: "后的数字(含逗号),捕获组([\d,]+)提取出带逗号的数字串,再通过替换操作移除逗号即可。

内容的提问来源于stack exchange,提问作者TityBoi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:15:44