python爬虫:bs4搜索文档树

news/2024/7/19 12:25:43 标签: 爬虫, python
python">'''
find:找一个
find_all:找多个

标签查找与属性查找:

    标签:
        - 字符串过滤器   字符串全局匹配
            name 属性匹配
            attrs 属性查找匹配
            text 文本匹配

        - 正则过滤器
            re模块匹配

        - 列表过滤器
            列表内的数据匹配

        - bool过滤器
            True匹配

        - 方法过滤器
            用于一些要的属性以及不需要的属性查找。

    属性:
        - class_
        - id
'''
html_doc = """
<html><head><title>The Dormouse's story</title></head><body>
<p class="sister"><b>$37</b></p><p class="story" id="p">
Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" >Elsie</a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie
</a>and they lived at the bottom of a well.</p><p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,'lxml') # find与find_all搜索文档 # soup.find() # name 属性匹配 # attrs 属性查找匹配 # text 文本匹配 ''' 字符串过滤器 ''' p = soup.find(name='p') p_s = soup.find_all(name='p') print(p) print(p_s) # name + attrs p = soup.find(name='p',attrs={"id":"p"}) print(p) # name + text tag = soup.find(name='title',text="The Dormous's story") print(tag) # name + attrs + text tag = soup.find(name='a',attrs={"class":"sisiter"},text='Elsie') print(tag) ''' - 正则过滤器 re模块匹配 ''' import re # name # 根据re模块匹配带有a的节点 a = soup.find(name=re._compile('a')) a_s = soup.find_all(name=re._compile('a')) print(a) print(a_s) # attrs a=soup.find(attrs={"id":re._compile('link')}) print(a) # - 列表过滤器 # 列表内的数据匹配 print(soup.find(name=['a','p','html',re._compile('a')])) print(soup.find_all(name=['a','p','html',re._compile('a')])) # - bool过滤器 # True匹配 print(soup.find(name=True,attrs={"id":True})) # - 方法过滤器 # 用于一些要的属性以及不需要的属性查找。 def have_id_not_class(tag): print(tag.name) if tag.name == 'p' and tag.has_attr("id") and not tag.has_attr("class"): return tag print(soup.find_all(name=函数对象)) print(soup.find_all(name=have_id_not_class)) # 补充知识点: # id a = soup.find(id='link2') print(a) # class p = soup.find(class_='sister') print(p)

  

转载于:https://www.cnblogs.com/Auraro997/p/11128209.html


http://www.niftyadmin.cn/n/680802.html

相关文章

linux系统镜像iso文件下载

linux系统镜像iso文件下载 首先你要选择你想要的linux版本&#xff0c;常见版本有CentOS&#xff0c;Ubuntu。选择一个你需要的。 有两个镜像站推荐&#xff1a; 网易镜像站&#xff1a;http://mirrors.163.com/  国外镜像站&#xff1a;http://mirrors.kernel.org/原文地址&…

HGOI 20190705 题解

Problem A 树状数组 给出数x&#xff0c;一直执行x x and (x1)-1 直到 x0 为止 询问这个数执行运算的次数。 这个数以二进制的形式表述出 x s1 & s2 .... s2 & s3 其中s2字段重复n次 &表示连接符号。 对于$100\%$的数据 $ length(s1),length(s2),length(s3) \leq…

html工作内容,【html5工作内容|工作职责|html5做什么】-看准网

企业介绍我们不是一个硬件生产企业&#xff0c;我们会生产出未来的汽车&#xff1b;我们不是一个移动应用开发商&#xff0c;我们会做出最炫最酷的车.生活的应用服务&#xff1b;我们不是一个解决方案提供商&#xff0c;我们会提供可供和车相关的最广泛的应用平台接入方案&…

Unity2018 Build AssetBundle过程中崩溃的解决办法

项目从Unity2017升级到了Unity2018.4&#xff0c;使用了新的嵌套Prefab功能&#xff0c;发现在打AssetBundle包的时候Unity崩溃。 找了好长时间原因&#xff0c;发现是新制作的Prefab中存在Missing Prefab&#xff0c;于是开发工具将Missing Prefab的Prefab找了出来&#xff0c…

python常用函数2

2、reduce()函数 reduce() 函数也是python内置的一个高阶函数。reduce()函数接收的参数和 map()相似&#xff0c;一个函数 f &#xff0c;一个list&#xff0c;但行为和 map()不同&#xff0c;reduce()传入的函数 f 必须接收两个参数&#xff0c;reduce()对list 的每个元素…

说明一下在Java中符号的运算表示,以及和都是怎么运算的。

如果我们现在有个运算是算2 << 3的结果是多少&#xff1f; 在这我们详细说明一下这种<<符号该如何运算。 “x<<y"是位运算符当中的"左移"运算&#xff0c;其中x是左移的数&#xff0c;y是左移的位。如&#xff1a;  2<<2的运算过程&a…

jQuery向上遍历DOM树之parents()、parent()及closest()的区别[转]

。。。。。。。。。。。。。。。。。。。。。 在这个sprint中&#xff0c;因为要写前端UI&#xff0c;所以用到了jQuery&#xff0c;但是jQuery在向上遍历DOM树的API中&#xff0c;有parents()、 parent()、closest()这几个&#xff0c;一直不太清楚它们具体的区别&#xff0c;…

Requests方法 --- json模块

1、Json 简介&#xff1a;Json&#xff0c;全名 JavaScript Object Notation&#xff0c;是一种轻量级的数据交换格式,常用于 http 请求中 2、可以用 help(json),查看对应的源码注释内容 Encoding basic Python object hierarchies::>>> import json>>> json…