python BS4(上)

本文介绍了Python的BeautifulSoup4库,用于从HTML或XML文件中提取数据。讲解了BeautifulSoup中的对象类型,如tag对象、NavigableString对象、bs4对象和comment对象,并详细阐述了遍历文档树的方法,包括遍历子节点、内容和父节点,以及遍历兄弟节点。同时,文章还介绍了如何通过find_all()和find()方法进行搜索树操作,包括各种过滤器的使用。
摘要由CSDN通过智能技术生成

bs4简介

  • 概念 —— Beautiful soup 是一个可以从HTML或XML文件中提取数据的网页信息提取库
  • bs里面有3种情况,第一遍历,第二查找,第三修改
  • bs4使用文档 —— http://www.crummy.com/software/BeautifulSoup/bs4/doc/
  • 用法:
    1. from bs4 import BeautifulSoup
    2. import bs4

对象种类:

类型 含义
tag 标签 —— 所有在<>内或在<>内且第一个空格前的字符串都是一个标签
NavigableString 可导航的字符串 —— 父类是字符串,可使用任何字符串的操作
BeautifulSoup bs对象 —— 父类是tag,可使用tag的所有方法
Comment 注释 —— 注释内容:<!–内容–>

tag对象

form bs4 import BeautifulSoup
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>
"""
bs = BeautifulSoup(html_doc,features='lxml')
print(bs.head)				# <head><title>The Dormouse's story</title></head>
print(type(bs.head))		# <class 'bs4.element.Tag'>
print(bs.a)					# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(type(bs.a))			# <class 'bs4.element.Tag'>

NavigableString对象

form bs4 import BeautifulSoup
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>
"""
bs = BeautifulSoup(html_doc,features='lxml')
print(bs.a.string)				# Elsie
print(type(bs.a.string))		# <class 'bs4.element.NavigableString'>

bs4对象

form bs4 import BeautifulSoup
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>
"""
bs = BeautifulSoup(html_doc,features='lxml')
print(bs)						# 内容为html_doc全部内容
print(type(bs))					# <class 'bs4.BeautifulSoup'>

comment对象

html =
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值