问题是我无法从 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 元素(在本例中为
<td>
元素)对于每个元素,使用 str.isnumber() 方法检查它是否包含数字:str.isdigit() doc
关于python - 如何仅打印 HTML 代码中的数字并对其求和?(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385655/