如何从HTML代码中只打印和求和数字?(Python)

如何从HTML代码中只打印和求和数字?(Python),python,html,numbers,Python,Html,Numbers,问题是我无法从HTML代码中提取数字 import re from bs4 import BeautifulSoup url = str(input()) soup = BeautifulSoup(url,"html.parser") data = soup.find_all('td') numbers = [d.text for d in data if d.text.isdigit()] # if the text of the td element is a number, includ

问题是我无法从HTML代码中提取数字

import re
from bs4 import BeautifulSoup

url = str(input())
soup = BeautifulSoup(url,"html.parser")
data = soup.find_all('td')
numbers = [d.text for d in data if d.text.isdigit()] # if the text of the td element is a number, include it in the list assigned to the variable 'numbers'
print(numbers)
>>> ['23', '40']
基本上,将其分解为更小的步骤:

  • 分离出可能包含您要查找的数据的所有HTML元素(在本例中为
    元素)

  • 对于这些元素中的每一个,使用str.isnumber()方法检查其是否包含数字:


立即尝试-必须绕过列表理解语法。这当然是可能的,但方法取决于您的需要。你可以不使用特定的或所有HTML解析库吗?isdigit不会考虑浮点数。@例如n1c9,该任务类似于此任务,但需要在不使用beautifulsoup的情况下执行此任务,只查找数字或替换,你能给我一些提示吗?你可以用类名注释查找。您提供的HTML字符串本身没有任何元素。我建议您阅读更多关于HTML和BeautifulSoup的内容