beautifulsoup4函数使用学习
来源:互联网 发布:电力数据网接入设备 编辑:程序博客网 时间:2024/05/17 04:34
beautifulsoup 用于提取抓取的html 字符串的标签
官网文档上的函数丰富,从上边找到的东西
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-parents-and-find-parent
1四类对象
Beautiful Soup 将复杂HTML文档转换成一个复杂的树形结构,每个节点都是 Python 对象,所有对象可以归纳为4种:
- Tag
- NavigableString
- BeautifulSoup
- Comment
2函数
find_all()
find()
find_parents() , find_parent()
find_next_siblings() ,find_next_sibling()
find_previous_siblings() ,find_previous_sibling()
find_all_next() ,find_next()
find_all_previous() , find_previous()
find(name,attrs,recursive,text,**wargs)
这些参数相当于过滤器一样可以进行筛选处理。
不同的参数过滤可以应用到以下情况:
一下查找方法可以组合查找
- 查找标签,基于name参数
- 查找文本,基于text参数
- 基于正则表达式的查找
- 查找标签的属性,基于attrs参数
- 基于函数的查找
html_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" id="link1">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>"""
soup=BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8')
基于标签 和 属性查找
link_node = soup.find('a',href='http://example.com/tillie')其中class为python关键字,关于class查找
查找class=‘mark’可以写成
1
link_node = soup.find(class_=mark)
2
link_node = soup.find(attrs={'class':'mark'})其中方法2也可以作为查找其他跟python关键字或者命名规则有冲突的属性查找
基于文本查找
link_node = soup.find(text="plants")
基于标签 和正则查找
link_node = soup.find('a',href=re.compile(r"lac"))
基于函数查找
def id_text_de(tag): return tag.name=='a' and tag.get_text()=='Elsie'link_node = soup.find(id_text_de)
find_all(name,attrs,recursive, string, limit, **kwargs)
find_all
就多了一个limit
跟数据库那个limit差不多
其他查找父子兄弟节点函数
相当于find缩小了查询范围,用法基本一致
阅读全文
0 0
- beautifulsoup4函数使用学习
- python爬虫——beautifulsoup4使用学习
- Beautifulsoup4学习笔记
- BeautifulSoup4的初步使用
- BeautifulSoup4 安装使用问题
- BeautifulSoup4
- BeautifulSoup4
- BeautifulSoup4的安装及使用
- BeautifulSoup4的安装及使用
- 使用beautifulsoup4使用lxml解析库报错
- 【Python学习】Mac+Eclipse 安装BeautifulSoup4
- (学习笔记)Python BeautifulSoup4 搜索部分
- Python3 BeautifulSoup4结合urllib简单使用
- BeautifulSoup4的安装和简单使用
- python中使用beautifulsoup4解析网页Html
- 爬虫——使用BeautifulSoup4的爬虫
- HTML解析之四:BeautifulSoup4的使用
- (学习笔记)Python BeautifulSoup4 取值部分
- winrar加密分析
- GC工作原理
- 列出所有有效的strerror串值
- robotframework
- PL/SQL(或者说是Oracle)错误ORA-00972:标识符过长
- beautifulsoup4函数使用学习
- Drupal ajax html和bootstrap dropdown button的事件机制
- 48. Rotate Image
- QT5容器遍历
- Spring Boot 入门
- QT 程序完全打开前的开启画面
- 关于 Bootstrap validator remote 的delay 属性 没效果
- 优化加号+操作符
- just4fun:神奇的代码生成好看的图片