BS4是一个解析库,能够经过某种解析器来帮咱们提取想要的数据html
由于他能够用简洁的语法快速提取用户想要的内容函数
# 安装BeautifulSoup4 pip3 install beautifulsoup4 # 安装解析器 # 根据官网解释,推荐使用lxml pip3 install lxml
from bs4 import BeautifulSoup 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> """ soup = BeautifulSoup(html_doc,'lxml') #自动补全html标签功能 print(soup) html_doc = soup.prettify() print(html_doc)
from bs4 import BeautifulSoup 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> """ soup = BeautifulSoup(html_doc,'lxml') # 一、直接使用 print(soup.html) print(type(soup.html)) #类型变成了element_tag print(soup.a) # 二、获取标签的名称 print(soup.a.name) # 三、获取标签的属性 print(soup.a.attrs) # 四、获取标签的内容 print(soup.p.text) # 五、嵌套选择 print(soup.html.body.p) # 六、子节点、子孙节点 print(soup.p.children) # 七、父节点、祖先节点 print(soup.b.parent) print(soup.b.parents) #返回一个生成器 # 生成器: def f(): yield 1 yield 2 yield 3 #把值放在生成器中 g=f() #expected results :<generator object parents at 0x000001C03E19E308> for line in g: print(line) #expected results 1 2 3 # 八、兄弟节点 (sibling: 兄弟姐妹) print(soup.a) # 获取下一个兄弟节点 print(soup.a.next_sibling) # 获取下一个的全部兄弟节点,返回的是一个生成器 print(soup.a.next_siblings) print(list(soup.a.next_siblings)) # 获取上一个兄弟节点 print(soup.a.previous_sibling) # 获取上一个的全部兄弟节点,返回的是一个生成器 print(list(soup.a.previous_siblings))
标签查找与属性查找: find与findall find找一个 findall找全部 标签: - 字符串过滤器 字符串全局匹配 name 属性匹配 attrs 属性查找匹配 text 文本匹配 - 正则过滤器 re模块匹配 - 列表过滤器 列表内的数据匹配 - bool过滤器 True匹配 - 方法过滤器 用于一些要的属性以及不须要的属性查找。 属性: - class_ - id
from bs4 import BeautifulSoup import re # 注意: 如何初始文本内有换行,也会算在里面。(坑) 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> """ # 第一个参数是解析文本 # 第二个参数是解析器 soup = BeautifulSoup(html_doc, 'lxml') ''' 标签查找与属性查找: 标签: - 字符串过滤器 字符串全局匹配 name 属性匹配 attrs 属性查找匹配 text 文本匹配 - 正则过滤器 re模块匹配 - 列表过滤器 列表内的数据匹配 - bool过滤器 True匹配 - 方法过滤器 用于一些要的属性以及不须要的属性查找。 属性: - class_ - id ''' # 一、字符串 # find的默认参数 第一个是name、第二个是attrs、第四个是text # name: 根据标签名匹配节点 print(soup.find('p')) # 获取第一个p标签 print(soup.find_all(name='p')) # 获取全部的p标签 # attrs: 根据属性查找匹配节点 print(soup.find(attrs={'id': 'p'})) # 查找id为p的标签 print(soup.find_all(attrs={'class': 'sister'})) # 查找class为sister的全部标签 # text: 根据文本匹配文档树内的文本 # 推荐配合其余匹配规则使用,不然毫无心义 print(soup.find(text='$37')) # 查找标签内为$37的文本 # name与text配合使用 print(soup.find_all(name='p', text='$37')) # 查找全部文本为$37的p标签 # name与attrs配合使用 print(soup.find(name='a', attrs={'id': 'link2'})) # 查找第一个id为link2的a标签 # attrs与text配合使用 print(soup.find_all(attrs={'id': 'link2'}, text='Lacie')) # 查找全部id为link2,文本为Lacie的标签 # name、attrs、text组合使用 print(soup.find_all(name='a', attrs={'id': 'link3'}, text='Tillie')) # 查找全部id为link3,文本为Tillie的a标签 # 二、正则 print(soup.find(name=re.compile('a'))) # 经过第一个标签名带有a的节点 print(soup.find_all(attrs={'id': re.compile('link')})) # 匹配全部id名带有link的节点 print(soup.find_all(text=re.compile('and'))) # 匹配全部文本带有"and"的节点 # 三、列表 (列表内能够匹配多个) print(soup.find_all(name=['a', re.compile('e')])) # 匹配全部a标签节点与全部标签中带有e的节点 print(soup.find_all(text=['$'])) # 找不到,由于$是精确查找 print(soup.find_all(text=['$37'])) # 查找$37文本,这样查找是没有意义的 print(soup.find_all(text=[re.compile('\$')])) # 正则中$是特殊字符,因此须要转义 # 四、bool print(soup.find_all(name=True)) # 查找全部有标签名的节点 print(soup.find_all(attrs={'id': True})) # 查找全部有id的节点 print(soup.find_all(text=True)) # 查找全部文本 # 五、方法 # 写一个只要有class没有id的a标签的函数 def has_class_not_id(arg): if arg.name == 'a' and arg.has_attr('class') and not arg.has_attr('id'): return arg.name print(soup.find_all(name=has_class_not_id)) # 经过has_class_not_id的函数匹配节点 # 六、标签与属性查找 # 标签 print(soup.find_all(attrs={'class': 'sister'})) # 属性 # 根据class属性查找,由于class是关键字,因此后面须要加下划线 print(soup.find_all(class_='sister')) # 根据id属性查找 print(soup.find_all(id='link2'))