BeautifulSoup模块解析html

Beautiful Soup是一个Python的第三方库,专门用于解析HTML文档。安装后,通过BeautifulSoup对象可以方便地查找和提取HTML元素。使用select()方法结合CSS选择器定位元素,get()方法获取元素属性值。通过这三个步骤,能高效地处理HTML页面数据。

Beautiful Soup是一个的三方模块,用于从HTML页面提取信息(用于这个目的时,它比正则表达式更好用)

安装 及导入

pip install beautifulsoup4#安装
import bs4#导入

接下来开始学习这个模块
在这里插入图片描述

  • 从HTML创建一个bs对象

bs.BeautifulSoup()函数调用时需要一个字符串,其中包含将要解析的HTML。
bs.BeautifulSoup() 函数返回一个 BeautifulSoup 对象,于是你的beautiful的对象就生成了
使用时需要保持计算机与互联网的连接

exampsoup=bs4.BeautifulSoup(examplefile)#示例

有了bs对象之后,就可以利用他的方法,定位HTML中的特定部分

  • 用select()方法查找元素
    针对要找的元素,调用方法,传入一个字符串作为CSS“选择器”,这样就可以取得web页面元素。
    选择器就像正则表达式:他们指定了要寻找的模式。
    常用的CSS选择器的模式
传递给select()方法的选择器将匹配…
soup.select(‘div’)所有名为<div 的元素
soup.select(‘#author’)带有id属性为author的元素
soup.select(’.notice’)所有使用CSS class属性名为notice 的元素
soup.select(‘div span’)所有在div元素之内的span元素
soup.select(‘input[name]’)所有名为input,并有一个name属性,其值无所谓的元素
soup.select(‘input[type=“button”]’)所有名为input,并有一个type属性,其值为button的元素

soup.select()方法将返回一个Tag对象的列表,Tag值可以传递给str()函数,显示他们代表的html标签。Tag值也可也有attrs属性,他将该Tag的所有html属性作为一个字典

<!-- This is the example.html file. -->

<html><head><title>The Website Title</title></head>
<body>
<p>Download my <strong>Python</strong> book from <a href="http://inventwithpython.com">my website</a>.</p>
<p class="slogan">Learn Python the easy way!</p>
<p>By <span id="author">Al Sweigart</span></p>
</body></html>

下面例1中用到的example.html就是上面的代码

import bs4
examplefile=open('example.html')
examplesoup=bs4.BeautifulSoup(examplefile.read(),"html.parser")#这里的read没有也可以,用requests返回值+.text
elms=examplesoup.select('#author')
print(type(elms))
print(len(elms))
print(elms[0].getText())
print(str(elms[0]))
print(elms[0].attrs)

执行结果如下

D:\recent\code\venv\Scripts\python.exe D:/recent/code/venv/test.py
<class 'bs4.element.ResultSet'>
1
Al Sweigart
<span id="author">Al Sweigart</span>
{'id': 'author'}

例2:

from bs4 import BeautifulSoup
s = '<dl><dt>今开</dt><dd class="s-down">3.87</dd></dl> <dl><dt>成交量</dt><dd>85.12万手</dd></dl>'
soup = BeautifulSoup(s,'html.parser')
list = soup.select('dl')#解析s中dl之间的元素
print(list[0].getText())
print(list[1].getText())

执行结果

D:\recent\code\venv\Scripts\python.exe D:/recent/code/venv/test.py
今开3.87
成交量85.12万手
  • 通过元素的属性获取数据
    Tag对象的get()方法让我们很容易从元素中获取属性值。向该方法传入一个属性名称的字符串,他将返回该属性的值

例3:把上面例子改一下(貌似没事实际意义)

from bs4 import BeautifulSoup
s = '<dl><dt>今开</dt><dd class="s-down">3.87</dd></dl> <dl><dt>成交量</dt><dd>85.12万手</dd></dl>'
soup = BeautifulSoup(s,'html.parser')
list = soup.select('dd')[0]
print(str(list))
print(list.get('class')[0])
print(list.attrs)
D:\recent\code\venv\Scripts\python.exe D:/recent/code/venv/test.py
<dd class="s-down">3.87</dd>
s-down
{'class': ['s-down']}

总结一下bs模块三步走

1、从html创建一个bs对象,对象=bs4.BeautifulSoup(’'html字符串“)
2、用select()方法寻找元素
3、通过元素的属性获取数据

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值