Python BeautifulSoup: Extract Paragraphs by Title from Local HTML File
This code demonstrates how to use Python's BeautifulSoup library to extract paragraphs associated with specific headings (h1-h6) from a local HTML file.
from bs4 import BeautifulSoup
def retrieve_paragraphs_by_title(html):
soup = BeautifulSoup(html, 'html.parser')
paragraphs = []
for title in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']):
paragraph_text = ''
for sibling in title.find_next_siblings():
if sibling.name.startswith('h'):
break
elif sibling.name == 'p':
paragraph_text += sibling.get_text()
paragraphs.append((title.get_text(), paragraph_text.strip()))
return paragraphs
# Read the local HTML file
with open('index.html', 'r') as file:
html = file.read()
# Process the HTML file
result = retrieve_paragraphs_by_title(html)
# Print the paragraphs
for title, paragraph in result:
print('Title:', title)
print('Paragraph:', paragraph)
print()
To modify the code to process the local HTML file 'index.html', you can use the open() function to read the file and pass its contents to the retrieve_paragraphs_by_title() function. Make sure to replace 'index.html' with the actual file path if it's in a different location.
原文地址: https://www.cveoy.top/t/topic/qc2Z 著作权归作者所有。请勿转载和采集!