BeautifulSoup
bs4主要使用find()方法和find_all()方法来搜索文档。
find()用来搜索单一数据,find_all()用来搜索多个数据

find_all()与find()

name –> tag名
string –> 内容
recursive –>是否搜索所有子孙节点 默认为true 设为false只搜索子节点

两方法用法相似这里以find_all()为例。

  1. #搜索tag名 <title></title>
  2. soup.find_all("title")
  3.  
  4. #关于属性
  5. #搜索id为"link2"的标签
  6. soup.find_all(id='link2')
  7.  
  8. #这里属性的值可以使用字符串,正则表达式 ,列表,True
  9. soup.find_all(id=re.compile("elsie"))
  10.  
  11. #可以指定多个条件
  12. soup.find_all(href=re.compile("elsie"), id='link1')
  13.  
  14. #对于有些不能指定的标签(data-foo)
  15. soup.find_all(attrs={"data-foo": "value"})
  16.  
  17. #对于class -->class为python保留字使用class_
  18. soup.find_all(class_="top")
  19. #属性结束
  20.  
  21. #关于string(内容)
  22. #基础 内容为'Elsie'的
  23. soup.find_all(string="Elsie")
  24.  
  25. #内容在数组中的
  26. soup.find_all(string=["Tillie", "Elsie", "Lacie"])
  27.  
  28. #内容匹配正则表达式的
  29. soup.find_all(string=re.compile("Dormouse"))
  30.  
  31. #匹配函数
  32. soup.find_all(string=is_the_only_string_within_a_tag)
  33. #内容结束
  34.  
  35. #搜索限制
  36. #限制搜索数量为2
  37. soup.find_all("a", limit=2)
  38.  
  39. #只搜索直接子节点
  40. soup.html.find_all("a", recursive=False)
  41. #搜索限制结束

简写

  1. soup.find_all("a")
  2. #等价于
  3. soup("a")
  1. soup.title.find_all(string=True)
  2. #等价于
  3. soup.title(string=True)

CSS选择器

  1. Beautiful Soup支持大部分的CSS选择器
  2.  
  3. #搜索tag为title
  4. soup.select("title")
  5.  
  6. #通过tag标签逐层查找
  7. soup.select("html head title")
  8.  
  9. #寻找直接子标签
  10. soup.select("head > title")
  11. soup.select("p > #link1")
  12.  
  13. #选择所有紧接着id为link1元素之后的class为sister的元素
  14. soup.select("#link1 + .sister")
  15.  
  16. #选择p元素之后的每一个ul元素
  17. soup.select("p + ul")
  18.  
  19. #同时用多种CSS选择器查询元素
  20. soup.select("#link1,#link2")
  21.  
  22. #通过查询元素属性
  23. soup.select('a[href="http://example.com/elsie"]')
  24. # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
  25.  
  26. soup.select('a[href^="http://example.com/"]')
  27. # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
  28. # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
  29. # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
  30.  
  31. soup.select('a[href$="tillie"]')
  32. # [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
  33.  
  34. soup.select('a[href*=".com/el"]')
  35. # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
  36. #通过查询元素属性结束
  37.  
  38. #通过语言查找
  39. soup.select('p[lang|=en]')
  40.  
  41. #查找第一个元素
  42. soup.select_one(".sister")

  

find_其他

find_parents() 和 find_parent()

搜索当前节点的父节点

  1. #查找一个标签a
  2. a = soup("a", id="link1")
  3.  
  4. #查找a的父节点中的P标签
  5. 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 回顾的更多相关文章

  1. python 基础回顾 一

    Python 基础回顾 可变类型:list ,dict 不可变类型:string,tuple,numbers tuple是不可变的,但是它包含的list dict是可变的. set 集合内部是唯一的 ...

  2. Python -bs4介绍

    https://cuiqingcai.com/1319.html Python -BS4详细介绍Python 在处理html方面有很多的优势,一般情况下是要先学习正则表达式的.在应用过程中有很多模块是 ...

  3. python bs4 + requests4 简单爬虫

    参考链接: bs4和requests的使用:https://www.cnblogs.com/baojinjin/p/6819389.html 安装pip:https://blog.csdn.net/z ...

  4. 全面进攻python之前回顾下自己近三个月的自学之路

    人生是在一直试错的过程中成长起来的.这句话貌似很有道理,但回顾了下自己近三个月python自学学习之路,又觉得自己对这句话又有了新的看法------行动之前必须要有正确的选择,这样做错了才能成长. 2 ...

  5. 零基础Python知识点回顾(一)

    如果你是小白,建议只要安装官网的python-3.7.0-amd64.exe 然后在电脑cmd命令提示符  输入检查是否已经安装pip,一般安装了python都会有的. >pip         ...

  6. python bs4 BeautifulSoup

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.bs4 模块的 BeautifulSoup 配合requests库可以写简单的爬虫. 安装 命令:pip in ...

  7. 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 ...

  8. python基础回顾1

    定义 tuple(元组), list (表) #!/usr/bin/env python # encoding: utf-8 a = 10 #定义一直变量,无需声明 s1 = (2,1.3,'love ...

  9. Python知识回顾 —— 面向对象

    博客转载自 http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/linhaifeng/articles/62040 ...

随机推荐

  1. Linux学习之shell

    通配符 *:表示从它所在位置开始到某个符合条件的结束符之间的任何字符 ?:表示它所在位置上的任何可能的单个字符 []:表示[]中所包含字符的任何一个 特殊键 ctrl+c  #停止当前程序执行 ctr ...

  2. D3---01基础的柱状图制作(转)

    ---文章转自 http://d3.decembercafe.org/index.html  ,Created by 十二月咖啡馆. 一个完整的柱形图包含三部分:矩形.文字.坐标轴. 首先要布置一个大 ...

  3. cookie设置域名问题,cookie跨域

    今天研究一天发现cookie无法设置除当前域名或者其父域名之外的其他domain. 这个是浏览器出于对cookie的保护造成的,也就是cookie无法跨域设置. 对于子域名也有如下规则,当前域名只能设 ...

  4. 禁止字符串 [POJ3691缩减版]

    题意考虑只由'A','G','C','T'四种字符组成的DNA字符串给定一个长度为k的字符串S,计算长度恰好为n的且不包含S的字符串的个数输入结果对10009取膜1<=k<=100,1&l ...

  5. VB输出数据到EXCEL

    Private Sub Command1_Click() Dim i As Long Dim j As Long , ) As Long Dim xlApp, WS, WB Set xlApp = C ...

  6. 虚拟机安装Linux系统

    Mware Workstation 12 序列号: 5A02H-AU243-TZJ49-GTC7K-3C61N 步骤一: 右键-->新建虚拟机 步骤二:自定义(高级)-->下一步 步骤三: ...

  7. 读取 ini 配置文件

    ini 配置文件格式:db_config.ini '''[section]option=value''' [DATABASE1] host=192.168.30.80 port= user=testa ...

  8. windows服务定时任务

    其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...

  9. Elasticsearch 滚动重启 必读

    关键词:elasticsearch , es , 滚动重启 , 禁止分片 由于之前es GC没有怎么调优,结果今天被大量scroll查询查挂了,GC 卡死了.然后为了先恢复给业务使用,也没什么其他办法 ...

  10. Java面经

    转载:[Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)   原文:http://www.cnblogs.com/wang-meng/p/5 ...