简体   繁体   中英

How to print and sum only numbers from HTML code?(Python)

问题是我无法从 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']

Essentially, break it down into smaller steps:

  • Separate out all of the HTML elements that could possibly contain the data you're after (in this case, <td> elements)

  • For each of those elements, check if it contains a digit using the str.isnumber() method: str.isdigit() doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM