Python Bs4 回顾
BeautifulSoup
bs4主要使用find()方法和find_all()方法来搜索文档。
find()用来搜索单一数据,find_all()用来搜索多个数据
find_all()与find()
name –> tag名
string –> 内容
recursive –>是否搜索所有子孙节点 默认为true 设为false只搜索子节点
两方法用法相似这里以find_all()为例。
#搜索tag名 <title></title>
soup.find_all("title") #关于属性
#搜索id为"link2"的标签
soup.find_all(id='link2') #这里属性的值可以使用字符串,正则表达式 ,列表,True
soup.find_all(id=re.compile("elsie")) #可以指定多个条件
soup.find_all(href=re.compile("elsie"), id='link1') #对于有些不能指定的标签(data-foo)
soup.find_all(attrs={"data-foo": "value"}) #对于class -->class为python保留字使用class_
soup.find_all(class_="top")
#属性结束 #关于string(内容)
#基础 内容为'Elsie'的
soup.find_all(string="Elsie") #内容在数组中的
soup.find_all(string=["Tillie", "Elsie", "Lacie"]) #内容匹配正则表达式的
soup.find_all(string=re.compile("Dormouse")) #匹配函数
soup.find_all(string=is_the_only_string_within_a_tag)
#内容结束 #搜索限制
#限制搜索数量为2
soup.find_all("a", limit=2) #只搜索直接子节点
soup.html.find_all("a", recursive=False)
#搜索限制结束
简写
soup.find_all("a")
#等价于
soup("a")
soup.title.find_all(string=True)
#等价于
soup.title(string=True)
CSS选择器
Beautiful Soup支持大部分的CSS选择器 #搜索tag为title
soup.select("title") #通过tag标签逐层查找
soup.select("html head title") #寻找直接子标签
soup.select("head > title")
soup.select("p > #link1") #选择所有紧接着id为link1元素之后的class为sister的元素
soup.select("#link1 + .sister") #选择p元素之后的每一个ul元素
soup.select("p + ul") #同时用多种CSS选择器查询元素
soup.select("#link1,#link2") #通过查询元素属性
soup.select('a[href="http://example.com/elsie"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>] soup.select('a[href^="http://example.com/"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.select('a[href$="tillie"]')
# [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.select('a[href*=".com/el"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
#通过查询元素属性结束 #通过语言查找
soup.select('p[lang|=en]') #查找第一个元素
soup.select_one(".sister")
find_其他
find_parents() 和 find_parent()
搜索当前节点的父节点
#查找一个标签a
a = soup("a", id="link1") #查找a的父节点中的P标签
a_string.find_parent("p")
find_next_siblings() 和 find_next_sibling()
搜索当前节点后边解析的兄弟节点
(可以理解为搜索当前标签下边的同级节点)
find_previous_siblings() 和 find_previous_sibling()
搜索当前节点前边解析的兄弟节点
(可以理解为搜索当前标签上边的同级节点)
find_all_next() 和 find_next()
对当前节点之后的节点进行迭代
find_all_previous() 和 find_previous()
对当前节点之前的节点进行迭代
Python Bs4 回顾的更多相关文章
- python 基础回顾 一
Python 基础回顾 可变类型:list ,dict 不可变类型:string,tuple,numbers tuple是不可变的,但是它包含的list dict是可变的. set 集合内部是唯一的 ...
- Python -bs4介绍
https://cuiqingcai.com/1319.html Python -BS4详细介绍Python 在处理html方面有很多的优势,一般情况下是要先学习正则表达式的.在应用过程中有很多模块是 ...
- python bs4 + requests4 简单爬虫
参考链接: bs4和requests的使用:https://www.cnblogs.com/baojinjin/p/6819389.html 安装pip:https://blog.csdn.net/z ...
- 全面进攻python之前回顾下自己近三个月的自学之路
人生是在一直试错的过程中成长起来的.这句话貌似很有道理,但回顾了下自己近三个月python自学学习之路,又觉得自己对这句话又有了新的看法------行动之前必须要有正确的选择,这样做错了才能成长. 2 ...
- 零基础Python知识点回顾(一)
如果你是小白,建议只要安装官网的python-3.7.0-amd64.exe 然后在电脑cmd命令提示符 输入检查是否已经安装pip,一般安装了python都会有的. >pip ...
- python bs4 BeautifulSoup
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.bs4 模块的 BeautifulSoup 配合requests库可以写简单的爬虫. 安装 命令:pip in ...
- python bs4解析网页时 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to inst(转)
Python小白,学习时候用到bs4解析网站,报错 bs4.FeatureNotFound: Couldn't find a tree builder with the features you re ...
- python基础回顾1
定义 tuple(元组), list (表) #!/usr/bin/env python # encoding: utf-8 a = 10 #定义一直变量,无需声明 s1 = (2,1.3,'love ...
- Python知识回顾 —— 面向对象
博客转载自 http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/linhaifeng/articles/62040 ...
随机推荐
- Linux操作系统log日志日志分别指什么
Linux操作系统log日志日志分别指什么 2019-04-20 20:41:05 一.一般的日志 /var/log/messages —包括整体系统信息,其中也包含系统启动期间的日志.此外,m ...
- <玩转Django2.0>读书笔记:表单
1. 表单字段 参考: 官方文档 Django表单字段汇总 2. 表单代码示例(forms.Form) # form.py代码 # 获取数据库数据 choices_list = [(i+1,v['ty ...
- BZOJ2143: 飞飞侠
2143: 飞飞侠 题意: 给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标 在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置 问三个人汇合所需要的最小总 ...
- selenium 不同版本Driver
selenium进行UI自动化测试需要Driver支持,不同的浏览器需要不同的Driver,之前使用的Driver可以正常运行,但是总会报一些莫名的问题,经过查找,原来IE的Driver需要与sele ...
- 多项式与三角函数求导——BUAA OO 第一单元作业总结
第一次作业 需求简要说明 针对符合规定的多项式表达式输出其符合格式规定的导函数多项式,格式错误输出WRONG FORMAT! 带符号整数 支持前导0的带符号整数,符号可省略,如: +02.-16> ...
- TypeScript 函数-函数类型
//指定参数类型 function add(x:number,y:number){ console.log("x:"+x); // reutrn(x+y); } //指定函数类型 ...
- php GD库快速消耗CPU资源漏洞 CVE-2018-5711测试
漏洞说明: 用一张GIF图片就可导致服务器发生崩溃直至宕机,在现实中非常容易利用. 影响版本: PHP 5 < 5.6.33 PHP 7.0 < 7.0.27 PHP 7.1 < 7 ...
- 使用Ant Build时提示错误: 编码GBK的不可映射字符
这个build.xml是由eclipse neon 2016.6生成的 我的情况是,所有文件都使用了UTF-8编码,build.xml第一行也好好写着UTF-8,但build时仍然有乱码,并且提示失败 ...
- rest_framework之认证源码剖析
如果我们写API有人能访问,有人不能访问,则需要些认证. 如何知道该用户是否已登入? 如果用户登入成功,则给用户一个随机字符串,去访问另一个页面. 以前写session的时候,都是把session写c ...
- ssh 报error: kex protocol error: type 30 seq 1
由于近期服务器升级了openssl,在使用navicat连接数据库报 查看日志 sshd[1990]: error: kex protocol error: type 30 seq 1 [preaut ...