BeautifulSoup常用语法有哪些

发布时间:2022-10-12 15:23:57 作者:iii
来源:亿速云 阅读:165

本篇内容主要讲解“BeautifulSoup常用语法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“BeautifulSoup常用语法有哪些”吧!

解析库

BeautifulSoup默认支持Python的标准HTML解析库,但是它也支持一些第三方的解析库

解析库使用方法优势劣势
Python标准库BeautifulSoup(html,’html.parser’)Python内置标准库;执行速度快容错能力较差
lxml HTML解析库BeautifulSoup(html,’lxml’)速度快;容错能力强需要安装,需要C语言库
lxml XML解析库BeautifulSoup(html,[‘lxml’,’xml’])速度快;容错能力强;支持XML格式需要C语言库
htm5lib解析库BeautifulSoup(html,’htm5llib’)以浏览器方式解析,最好的容错性速度慢

代码示例

demo html

<!DOCTYPE html>
<!--STATUS OK-->
<html>
 <head>
  <meta content="text/html;charset=utf-8" http-equiv="content-type"/>
  <meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
  <meta content="always" name="referrer"/>
  <link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/>
  <title>百度一下,你就知道 </title>
 </head>
 <body link="#0000cc">
  <div id="wrapper">
   <div id="head">
    <div class="head_wrapper">
     <div id="u1">
      <a rel="nofollow" class="mnav" href="http://news.baidu.com" name="tj_trnews">
       新闻 </a>
      <a rel="nofollow" class="mnav" href="https://www.hao123.com" name="tj_trhao123">
       hao123 </a>
      <a rel="nofollow" class="mnav" href="http://map.baidu.com" name="tj_trmap">
       地图 </a>
      <a rel="nofollow" class="mnav" href="http://v.baidu.com" name="tj_trvideo">
       视频 </a>
      <a rel="nofollow" class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">
       贴吧 </a>
      <a rel="nofollow" class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">
       更多产品 </a>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>

创建beautifulsoup4对象

from bs4 import BeautifulSoup

soup = BeautifulSoup(html,"html.parser")

# 缩进格式
print(soup.prettify())

# 获取title标签的所有内容
print(soup.title)
#Output:<title>百度一下,你就知道 </title>

# 获取title标签的名称
print(soup.title.name)
#Output:title

# 获取title标签的文本内容
print(soup.title.string)
#Output:百度一下,你就知道

# 获取head标签的所有内容
print(soup.head)

# 获取第一个div标签中的所有内容
print(soup.div)

# 获取第一个div标签的id的值
print(soup.div["id"])

# 获取第一个a标签中的所有内容
print(soup.a)

# 获取所有的a标签中的所有内容
print(soup.find_all("a"))

# 获取id="u1"
print(soup.find(id="u1"))

# 获取所有的a标签,并遍历打印a标签中的href的值
for item in soup.find_all("a"):
	print(item.get("href"))

# 获取所有的a标签,并遍历打印a标签的文本值
for item in soup.find_all("a"):
	print(item.get_text())

BeautifulSoup4四大对象种类

BeautifulSoup4将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:

遍历文档树

# tag的.content 属性可以将tag的子节点以列表的方式输出
print(soup.head.contents)

# 用列表索引来获取它的某一个元素
print(soup.head.contents[1])
for child in  soup.body.children:
    print(child)

搜索文档树

返回符合条件的第一个Tag,即当我们要取一个值的时候就可以用这个方法

t = soup.div.div

# 等价于
t = soup.find("div").find("div")

CSS选择器

print(soup.select('title'))

print(soup.select('a'))
print(soup.select('.mnav'))
print(soup.select('#u1'))
print(soup.select('div .bri'))
print(soup.select('a[class="bri"]'))
print(soup.select('a[href="http://tieba.baidu.com"]'))
item_list = soup.select("title")
print(soup.select('title')[0].get_text())

到此,相信大家对“BeautifulSoup常用语法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. MySQL常用语法
  2. 配置 BeautifulSoup

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

beautifulsoup

上一篇:Python多进程怎么应用

下一篇:python dumpdata怎么按条件导出数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》