如何从HTML代码中只打印和求和数字?(Python)
问题是我无法从HTML代码中提取数字如何从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
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()方法检查其是否包含数字: