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学习之shell
通配符 *:表示从它所在位置开始到某个符合条件的结束符之间的任何字符 ?:表示它所在位置上的任何可能的单个字符 []:表示[]中所包含字符的任何一个 特殊键 ctrl+c #停止当前程序执行 ctr ...
- D3---01基础的柱状图制作(转)
---文章转自 http://d3.decembercafe.org/index.html ,Created by 十二月咖啡馆. 一个完整的柱形图包含三部分:矩形.文字.坐标轴. 首先要布置一个大 ...
- cookie设置域名问题,cookie跨域
今天研究一天发现cookie无法设置除当前域名或者其父域名之外的其他domain. 这个是浏览器出于对cookie的保护造成的,也就是cookie无法跨域设置. 对于子域名也有如下规则,当前域名只能设 ...
- 禁止字符串 [POJ3691缩减版]
题意考虑只由'A','G','C','T'四种字符组成的DNA字符串给定一个长度为k的字符串S,计算长度恰好为n的且不包含S的字符串的个数输入结果对10009取膜1<=k<=100,1&l ...
- VB输出数据到EXCEL
Private Sub Command1_Click() Dim i As Long Dim j As Long , ) As Long Dim xlApp, WS, WB Set xlApp = C ...
- 虚拟机安装Linux系统
Mware Workstation 12 序列号: 5A02H-AU243-TZJ49-GTC7K-3C61N 步骤一: 右键-->新建虚拟机 步骤二:自定义(高级)-->下一步 步骤三: ...
- 读取 ini 配置文件
ini 配置文件格式:db_config.ini '''[section]option=value''' [DATABASE1] host=192.168.30.80 port= user=testa ...
- windows服务定时任务
其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...
- Elasticsearch 滚动重启 必读
关键词:elasticsearch , es , 滚动重启 , 禁止分片 由于之前es GC没有怎么调优,结果今天被大量scroll查询查挂了,GC 卡死了.然后为了先恢复给业务使用,也没什么其他办法 ...
- Java面经
转载:[Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等) 原文:http://www.cnblogs.com/wang-meng/p/5 ...