Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách xóa thẻ rỗng (empty tags) bằng BeautifulSoup trong Python?

BeautifulSoup là thư viện parse HTML/XML, trích xuất dữ liệu. Cũng có thể xóa thẻ rỗng trong tài liệu HTML/XML.

Cài đặt: pip install beautifulsoup4

Ví dụ

# Import BeautifulSoup
from bs4 import BeautifulSoup

# HTML mẫu
html_object = """

Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.

""" # Tạo soup object soup = BeautifulSoup(html_object, "lxml") # Duyệt và xóa thẻ rỗng for x in soup.find_all(): if len(x.get_text(strip=True)) == 0: x.extract() print(soup)

Kết quả

Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.