python爬虫-bs4

python爬虫-bs4

目录

说明

BeautifulSoup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据

在爬虫项目中经常会遇到不规范、及其复杂的HTML代码

BeautifulSoup4提供了强大的方法来遍历文档的节点以及根据各种条件搜索和过滤文档中的元素。你可以使用CSS选择器、正则表达式等灵活的方式来定位和提取所需的数据

安装

shell 复制代码
pip install BeautiifulSoup4

导入

python 复制代码
from bs4 import BeautifulSoup

基础用法

解析对象

python 复制代码
soup = BeautifulSoup('目标数据','解析器')

目前有三种主流解析器

  • html.parser
  • lxml(推荐)
  • html5lib

获取文本

获取文本的方法两种方式textcontents

contents

python 复制代码
from bs4 import BeautifulSoup

data = """
<h1>Welcome to BeautifulSoup Practice</h1>
    <div class="article">
        <h2>Article Title</h2>
        <p>This is a paragraph of text for practicing BeautifulSoup.</p>
        <a href="https://www.example.com">Link to Example Website</a>
"""
soup = BeautifulSoup(data, 'lxml')
print(soup.contents)
# 输出:
"""
[<html><body><h1>Welcome to BeautifulSoup Practice</h1>
<div class="article">
<h2>Article Title</h2>
<p>This is a paragraph of text for practicing BeautifulSoup.</p>
<a href="https://www.example.com">Link to Example Website</a>
</div></body></html>]
"""

text

python 复制代码
print(soup.text)
"""
Welcome to BeautifulSoup Practice

Article Title
This is a paragraph of text for practicing BeautifulSoup.
Link to Example Website
"""

Tag对象

获取HTML中的标签内容

比如<p> <div>

示例:

python 复制代码
print(soup.h2)
# <h2>Article Title</h2>

print(soup.h2.text)
# Article Title
find参数

获取class要加下划线,因为在python中它属于关键字,除了class还可以换成任意属性名

python 复制代码
data = """
<h1>Welcome to BeautifulSoup Practice</h1>
    <div class="article">
        <p>This is a paragraph of text for practicing BeautifulSoup.</p>
    </div>
    <div class="ex2">
        <p>This is a abcd.</p>
    </div>
"""
soup = BeautifulSoup(data, 'lxml')
print(soup.find('div', class_='article'))
获取标签属性
python 复制代码
data = ' <p id = "apple">This is a paragraph of text for practicing BeautifulSoup.</p>'
soup = BeautifulSoup(data, 'lxml')
tag = soup.find('p')
print(tag.get('id'))
# apple
获取所有标签
python 复制代码
soup = BeautifulSoup(data, 'lxml')
print(soup.find_all('p'))
# [<p>This is a paragraph of text for practicing BeautifulSoup.</p>, <p>This is a abcd.</p>]

print(len(soup.find_all('p')))
# 2

括号为空则获取全部标签

获取标签名
python 复制代码
print(soup.div.name)
# div
嵌套获取

示例HTML如下

python 复制代码
html = '''
<div class="article">
    <h2>Article Title</h2>
    <p>This is a paragraph of text for practicing BeautifulSoup.</p>
    <p>This is a abcd.</p>
    <a href="https://www.example.com">Link to Example Website</a>
</div>
'''

目标:获取div下的所有p标签内容

python 复制代码
print(soup.find('div', class_='article').find_all('p'))
子节点和父节点
python 复制代码
soup = BeautifulSoup(data, 'lxml')
# 遍历获取所有父节点
for item in soup.p.parents:
    print(item)

# 遍历获取所有子节点
for i in soup.p.children:
    print(soup.p.children)
相关推荐
用户7783366132116 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
65岁退休Coder10 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文13 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes13 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明13 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn13 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困13 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao15 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼15 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君15 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python