- Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。
- Beautiful Soup已成为和lxml、html5lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度
- 使用
- 首先导入bs4库,创建BeautifulSoup对象。
-
soup的用法
标签中有name 和 attrs,可以利用soup加 标签名 轻松地获取这些标签的内容
( 注:在查找的是在所有内容中的第一个符合要求的标签)-
代码: import requests from bs4 import BeautifulSoup url = 'https://www.mzitu.com/' req = requests.get(url) html = req.content.decode('utf-8') # 创建BeautifulSoup对象 obj = BeautifulSoup(html,'lxml') # print(obj.prettify()) 格式化输出 tittle = obj.a con = obj.img.attrs.get('src')# 获取img标签下的属性值并获取src对应的值 print(con) 结果: https://i.meizitu.net/pfiles/img/lazy.png
- 子节点。 .contents .children 属性
tag 的 .content 属性可以将tag的子节点以列表的方式输出,
通过tag的 .children 生成器,可以对tag的子节点进行循环 - 子孙节点。.descentdants 属性
.contents 和 .children 属性仅包含tag的直接子节点,
.descendants 属性可以对所有tag的子孙节点进行递归循环,
和 children类似,我们也需要遍历获取其中的内容 - .string 获取单个节点内容
如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。
如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容 - .strings .stripped_strings 获取多个节点内容
如果tag中包含多个字符串,可以使用 .strings 来循环获取,
但是输出的字符串中可能包含了很多空格或空行,就可以使用 .stripped_strings 可以去除多余空白内容 - .Parent .parents 父节点
.parent属性来获取某个元素的父节点, .parents属性可以递归得到元素的所有父辈节点 - 兄弟节点 .next_sibling .previous_sibling 属性
.next_sibling 属性获取了该节点的下一个兄弟节点,
.previous_sibling 则与之相反,如果节点不存在,则返回 None.
next_siblings .previous_siblings 属性 通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出
注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白,
因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行 - 前后节点 .next_element .previous_element 属性
.next_element指向解析过程中下一个被解析的对象(字符串或tag),
.previous_element指向当前被解析的对象的前一个解析对象
.next_elements .previous_elements 通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样
-
-
搜索文档树 着重介绍find()和find_all()
find_all()方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件。
Find()和find_all()的区别就是,find直接返回元素的一个结果,find_all返回元素列表- find_all( name , attrs , recursive , text , **kwargs )简介一下参数:
- name 参数可以查找所有名字为name的tag(标签),字符串对象会被自动忽略掉;name参数可以传入字符串、正则表达式、列表、True、自定义的方法等但是各自代表的含义不一样。
- 字符串,在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容。
- 正则表达式,Beautiful Soup会通过正则表达式的match()来匹配内容
- 列表,Beautiful Soup会将与列表中任一元素匹配的内容返回
例如: soup.find_all(re.compile("^b")) - True 将会匹配所有的tag
- 传入自定义方法,如果方法返回true,这个方法的匹配内容已经找到,否则返回false
- Keywords 参数如果一个指定名字的参数不是搜索内置的参数名,
搜索时会把该参数当作指定名字tag的属性来搜索。- 如果包含一个名字为 id 的参数,Beautiful Soup会搜索每个tag的”id”属性
- 如果传入 href 参数,Beautiful Soup会搜索每个tag的”href”属性
- 使用多个指定名字的参数可以同时过滤tag的多个属性
- 对于class ,可以使用class_来搜索
- 对于某些tag属性不能通过搜索得到的额,可以使用attrs参数得到
- find_all( name , attrs , recursive , text , **kwargs )简介一下参数:
-
Css选择器
在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),
返回类型是list(列表)。- 通过标签名查找
代码:import requests from bs4 import BeautifulSoup url = 'https://www.douban.com/group/explore' req = requests.get(url) html = req.content.decode('utf-8') # 创建BeautifulSoup对象 obj = BeautifulSoup(html,'lxml') # print(obj.prettify()) 格式化输出 con = obj.select('head') print(con) 结果: [<head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> <meta content="ok0wCgT20tBBgo9_zat2iAcimtN4Ftf5ccsh092Xeyw" name="google-site-verification"/> <title> 讨论精选 (豆瓣) ··· </head>]
- 通过类名查找(类名前面加点)
代码:con = obj.select('.title') 结果: [<div class="title"> <a class="" href="https://www.douban.com/group/198653/">美国代购</a> ······ <a class="" href="https://www.douban.com/group/34020/">烘焙之家</a> </div>] Process finished with exit code 0
- 通过id 查找(ID名前面加#)
代码:con = obj.select('#content') 结果: [<div id="content"> <h1><div class="head-nav"> ······ </div>] Process finished with exit code 0
- 通过组合查找。组合查找和写Css样式与案例一样。通过标签名、类名、Id名进行组合。
需求:查找a标签下class为item-qq的值
代码:con = obj.select('a #item-qq') 结果: ······ content="ok0wCgT20tBBgo9_zat2iAcimtN4Ftf5ccsh092Xeyw" name="google-site-verification"/> <title> (豆瓣) </title> ··· </html>
- head的子标签
代码:con = obj.select('head>title') 结果: ··· <li><a href="https://www.douban.com/group/explore">精选</a></li> <li><a href="https://www.douban.com/group/explore/culture">文化</a></li> <li><a href="https://www.douban.com/group/explore/travel">行摄</a></li> <li><a href="https://www.douban.com/group/explore/ent">娱乐</a></li> <li><a href="https://www.douban.com/group/explore/fashion">时尚</a></li> <li><a href="https://www.douban.com/group/explore/life">生活</a></li> <li><a href="https://www.douban.com/group/explore/tech">科技</a></li> ···
- 属性查找。
代码:con = obj.select('scrip[defer="defer"]') (注意:在匹配规则中单引号和双引号要区分开) 结果: ···<a href="https://www.douban.com/about">关于豆瓣</a> · <a href="https://www.douban.com/jobs">在豆瓣工作</a> · <a href="https://www.douban.com/about?topic=contactus">联系我们</a> · <a href="https://www.douban.com/about?policy=disclaimer">免责声明</a> · <a href="https://help.douban.com/group" target="_blank">帮助中心</a> · <a href="https://www.douban.com/doubanapp/">移动应用</a> · <a href="https://www.douban.com/partner/">豆瓣广告</a> </span>
注意:属性组合查找,不在同一节点的空格隔开,同一节点的不加空格
以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容 - 查找时还可以加入属性元素,属性需要用中括号括起来,
注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。
- 通过标签名查找
-
- 首先导入bs4库,创建BeautifulSoup对象。
BeautifulSoup的大部分的技术点都总结了,有不足之处,欢迎来电。。。——————>电话号码是:
不告诉你