from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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) #打印一下 soup 对象的内容,格式化输出 print(soup.prettify()) #=================================== #(1)Tag #获取标签内容 #soap+标签 查找的是在所有内容中的第一个符合要求的标签 print(soup.title) #<title>The Dormouse's story</title> print(soup.head) print(soup.a) print(soup.p) #<p class="title" name="dromouse"><b>The Dormouse's story</b></p> #验证对象类型 print(type(soup.a)) ##<class 'bs4.element.Tag'> #把 p 标签的所有属性打印输出了出来 print(soup.p.attrs) # {'class': ['title'], 'name': 'dromouse'} #Tag的name属性 print(soup.name) print(soup.head.name) #[document] #head #单独获取某个属性 print(soup.p['class']) #['title'] print(soup.p.get('class')) #['title'] #更改或新增属性内容 soup.p['class']="newClass" print(soup.p) #<p class="newClass" name="dromouse"><b>The Dormouse's story</b></p> #删除属性 del soup.p['class'] print(soup.p) #<p name="dromouse"><b>The Dormouse's story</b></p> #============================= #(2)NavigableString #获取标签内容 print(soup.p.string) #The Dormouse's story print(soup.a.string) #Elsie #标签内容类型 print(type(soup.p.string)) ##<class 'bs4.element.NavigableString'> #============================= #(3)BeautifulSoup #BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性来感受一下 print(type(soup.name)) #<type 'unicode'> print(soup.name) # [document] print(soup.attrs) #{} 空字典 #============================= #(4)Comment #Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号 print(soup.a) #<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a> print(soup.a.string) #Elsie print(type(soup.a.string)) #<class 'bs4.element.Comment'>
遍历文档树
from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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) print("==========================================") #遍历文档树 #(1)直接子节点.contents .children #.contents 和 .children 属性仅包含tag的直接子节点 print(soup.head.contents) #[<title>The Dormouse's story</title>] print(soup.head.contents[0] ) #<title>The Dormouse's story</title> print(soup.head.contents[0].string ) #The Dormouse's story print(soup.head.children) #<listiterator object at 0x7f71457f5710> for child in soup.head.children: print(child) #<title>The Dormouse's story</title> print("=======================1") for child in soup.p.children: print(child) #<b>The Dormouse's story</b> print("=======================2") for child in soup.body.children: print(child) print("==========================================") #(2)所有子孙节点.descendants for child in soup.descendants: print(child) #(3)节点内容:.string 属性 #如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。 print(soup.head.string) #The Dormouse's story print(soup.title.string) #The Dormouse's story #如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None print(soup.html.string) # None #(4)多个内容 .strings .stripped_strings 属性 #for string in soup.strings: # print(repr(string)) #输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容 for string in soup.stripped_strings: print(repr(string)) #(5)父节点 .parent 属性 p = soup.p print(p.parent.name) #body print(soup.title.parent.name ) #head #(6)全部父节点 .parents content = soup.head.title.string for parent in content.parents: print(parent.name) #title #head #html #[document] #(7)兄弟节点 #知识点:.next_sibling .previous_sibling 属性 #兄弟节点可以理解为和本节点处在统一级的节点,.next_sibling 属性获取了该节点的下一个兄弟节点,.previous_sibling 则与之相反,如果节点不存在,则返回 None #注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白,因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行 print soup.p.next_sibling # 实际该处为空白 print soup.p.prev_sibling #None 没有前一个兄弟节点,返回 None print soup.p.next_sibling.next_sibling #<p class="story">Once upon a time there were three little sisters; and their names were #<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, #<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and #<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; #and they lived at the bottom of a well.</p> #(8)全部兄弟节点 #知识点:.next_siblings .previous_siblings 属性 for sibling in soup.a.next_siblings: print(repr(sibling)) #(9)前后节点 #知识点:.next_element .previous_element 属性 #与 .next_sibling .previous_sibling 不同,它并不是针对于兄弟节点,而是在所有节点,不分层次 #<head><title>The Dormouse's story</title></head> #那么它的下一个节点便是 title,它是不分层次关系的 print(soup.head.next_element) #<title>The Dormouse's story</title> #(10)所有前后节点 #知识点:.next_elements .previous_elements 属性 for element in last_a_tag.next_elements: print(repr(element))
。。
相关推荐
Beautiful Soup主要应用于爬虫开发中,用于解析HTML页面,从中提取数据,如爬取网站上的新闻、商品信息、图片等内容,以及对数据进行分析、挖掘、可视化等处理。同时,也可以用Beautiful Soup来处理XML文档,如解析...
Python 爬虫入门的教程之Beautiful Soup解析
Python HTML解析库Beautiful Soup https://xercis.blog.csdn.net/article/details/107041445
Beautifulsoup4 是一个强大的Python爬虫web解析库,可以用来对get到的网页进行解析。
Python 使用Beautiful Soup 爬虫教程 Beautiful Soup是一个可以从HTML或XML中提取数据的Python库。它可以通过你喜欢的转换器快速帮你解析并查找整个HTML文档。 Beautiful Soup自动将输入文档转为Unicode编码,输出...
python爬虫模块Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下: Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能...
python爬虫-Beautiful Soup库入门说明Beautiful Soup库安装Beautiful Soup库的引用Beautiful Soup库解析器Beautiful Soup类的基本元素例子tag标签tag的name(名字)tag的Attributes(属性)Tag的NavigableString...
目录1. 简介2....最新版本Beautiful Soup 4 简称bs4。优势:相比于ET库, 功能更全,可以选择解析器来解析文档,既支持html, 也支持xml,容错度(简单理解为文档格式自动补全功能)也更高,API也很好用。 2
Beautiful Soup4.2中文文档,快速解析HTML网页,使用非常简单。
要想学好爬虫,必须把基础打扎实,之前发布了两篇文章,分别是使用XPATH和requests爬取网页,今天的文章是学习Beautiful Soup并通过一个例子来实现如何使用Beautiful Soup爬取网页。 什么是Beautiful Soup ...
常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用...
常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用...
常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用...
主要讲解BeautifulSoup的安装基本概论说明以及解析器lxml的安装
文章目录Beautiful Soup4 简介Beautiful Soup4 解析器Beautiful Soup4 安装Beautiful Soup4 解析器安装Beautiful Soup4 简单使用Beautiful Soup4 四大对象TagNavigableStringBeautifulSoupComment Beautiful Soup4 ...
常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用...
Beautiful Soup 库 Beautiful Soup 的使用。 >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(' data ',"html.parser") '''parser为解析器。具体啥意思不知道。。。''' 实际操作: >>> import requests...
Beautiful Soup: Beautiful Soup 是一个HTML和XML解析库,可以方便地从网页中提取所需的数据。它支持多种解析器,并提供简单易用的API来处理网页内容。 Requests: Requests 是一个简洁而优雅的HTTP库,用于发送和...
这段代码会发送 HTTP 请求,获取网页内容,并使用 Beautiful Soup 解析 HTML 代码。然后,它会提取网页标题并打印出来。 希望这个示例代码能帮到您。 requests,BeautifulSoup 这段代码会发送 HTTP 请求,获取网页...