使用 python 和 beautifulsoup 解析 html 文档的方法如下:加载 html 文档并创建 beautifulsoup 对象。使用 beautifulsoup 对象查找和处理标签元素,如:查找特定标签:soup.find(tag_name)查找所有特定标签:soup.find_all(tag_name)查找具有特定属性的标签:soup.find(tag_name, {'attribute': 'value'})提取标签的文本内容或属性值。根据需要调整代码以获取特定信息。

目标:
了解如何使用 Python 和 BeautifulSoup 库解析 HTML 文档。
必备知识:
代码:
立即学习“前端免费学习笔记(深入)”;
from bs4 import BeautifulSoup
# 加载 HTML 文档
html_doc = """
<html>
<head>
<title>HTML 文档</title>
</head>
<body>
<h1>标题</h1>
<p>段落</p>
</body>
</html>
"""
# 创建 BeautifulSoup 对象
soup = BeautifulSoup(html_doc, 'html.parser')
# 获取标题标签
title_tag = soup.find('title')
print(title_tag.text) # 输出:HTML 文档
# 获取所有段落标签
paragraph_tags = soup.find_all('p')
for paragraph in paragraph_tags:
print(paragraph.text) # 输出:段落
# 获取特定属性的值
link_tag = soup.find('link', {'rel': 'stylesheet'})
print(link_tag['href']) # 输出:样式表链接实战案例:
一个简单的实战案例是使用 BeautifulSoup 从网页中提取指定信息的爬虫。例如,你可以使用以下代码从 Stack Overflow 中提取问题和答案:
import requests
from bs4 import BeautifulSoup
url = 'https://stackoverflow.com/questions/31207139/using-beautifulsoup-to-extract-specific-attribute'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
questions = soup.find_all('div', {'class': 'question-summary'})
for question in questions:
question_title = question.find('a', {'class': 'question-hyperlink'}).text
question_body = question.find('div', {'class': 'question-snippet'}).text
print(f'问题标题:{question_title}')
print(f'问题内容:{question_body}')
print('---')这只是使用 BeautifulSoup 解析 HTML 文档的众多示例之一。你可以根据具体需求调整代码以获取不同的信息。
以上就是HTML 段落自动缩进两空格的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号