[学习笔记]Beautiful Soup语法基本使用

1. Beautiful_Soup语法

Beautiful_Soup语法
find all搜索的是全部节点,find搜索的是满足条件的第一个节点

2.获取网页信息

获取网页信息

思路如下

# <a href = "123.html" class = 'article_link'> Python </a>

# 根据 HTML 网页字符串创建 BeautifulSoup 对象
soup = BeautifulSoup(
    html_doc,  # HTML 文档字符串
    'html.parser',  # HTML 解析器
    from_encoding='utf8')  # HTML 文档的编码

# 查找所有标签为 a 的节点
soup.find_all('a')

# 查找所有标签为 a,链接符合 /view/123.html 形式的节点
soup.find_all('a', href='/view/123.htm')
soup.find_all('a', href=re.compile(r'/view/\d+\.htm'))

# 查找所有标签为div,class,为 abc, 文字为 Python 的节点
soup.find_all('div', class_='abc', string='Python')

# 得到节点 <a href = '1.html'>Python</a>

# 获取查找到的节点的标签名称
node.name

# 获取查找到的节点的href 属性
node['href']

# 获取查找到的节点的链接文字
node.get_text()

3.编码

3.1 材料准备

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
3.2 解析材料数据

运行示例

soup = BeautifulSoup(html_doc, "html.parser", from_encoding='utf8')

print("获取所有的链接")
links = soup.find_all('a')

for link in links:
    print(link.name, link['href'], link.get_text())

运行结果

获取所有的链接
a http://example.com/elsie Elsie
a http://example.com/lacie Lacie
a http://example.com/tillie Tillie

获取单一链接数据

print("获取 http://example.com/lacie 的链接")
link_node = soup.find('a', href="http://example.com/lacie")
print(link_node.name, link_node['href'], link_node.get_text())

运行示例

获取 http://example.com/lacie 的链接
a http://example.com/lacie Lacie

使用正则表达式

print("正则表达式")
link_node = soup.find('a', href=re.compile(r'sie'))
print(link_node.name, link_node['href'], link_node.get_text())

运行结果

正则表达式
a http://example.com/elsie Elsie

根据 p 段落 class 的内容

print("根据 p 段落 class 的内容")
# class_ 需要加下划线
p_node = soup.find('p', class_="title")
print(p_node.name, p_node.get_text())
根据 p 段落 class 的内容
p The Dormouse's story
3.3 完整运行示例
# coding:utf8
from bs4 import BeautifulSoup
import re

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""


soup = BeautifulSoup(html_doc, "html.parser", from_encoding='utf8')

print("获取所有的链接")
links = soup.find_all('a')

for link in links:
    print(link.name, link['href'], link.get_text())

print("获取 http://example.com/lacie 的链接")
link_node = soup.find('a', href="http://example.com/lacie")
print(link_node.name, link_node['href'], link_node.get_text())

print("正则表达式")
link_node = soup.find('a', href=re.compile(r'sie'))
print(link_node.name, link_node['href'], link_node.get_text())

print("根据 p 段落 class 的内容")
# class_ 需要加下划线
p_node = soup.find('p', class_="title")
print(p_node.name, p_node.get_text())

运行结果

a http://example.com/lacie Lacie
a http://example.com/tillie Tillie
获取 http://example.com/lacie 的链接
a http://example.com/lacie Lacie
正则表达式
a http://example.com/elsie Elsie
根据 p 段落 class 的内容
p The Dormouse's story
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值