爬虫之BS4模块

BS4模块简介

Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为tiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。
你不需要考虑编码方式,除非文档没有指定一个编一下原始编码方式就可以了。

bs4的四种对象

  1. BeautifulSoup对象
  2. Tag对象
    Tag就是html中的一个标签,用BeautifulSoup就能解析出来Tag的具体内容,
    具体的格式为Soup.name,其中name就是html下的标签。

举例:

html = """
<html>
<head><title>story12345</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"><span>westos</span><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister1" 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,'html.parser')

标签

1.根据标签获取内容
根据格式化如果title只有一个,根据标签可以获取

print(soup.title)
print(type(soup.title))
print(soup.title.name)

在这里插入图片描述

获取标签内的属性信息

print(soup.a.attrs)
print(soup.a.attrs[‘href’])

在这里插入图片描述
2.标签常用方法
get方法用于得到标签下的属性值,注意这是一个重要的方法,在许多场合都能用到,比如你要得到<img src=”#”>标签下的图像url,那么就可以用soup.img.get(‘src’)
string得到标签下的文本内容,只有在此标签下没有子标签,或者只有一个子标签的情况下才能返回其中的内容,否则返回的是None;
get_text()可以获得一个标签中的所有文本内容,包括子孙节点的内容,这是最常用的方法。

print(soup.a.get(‘href’))
print(soup.a.get(‘class’))
print(soup.a.string) #由于a标签下有多个子标签,所以应返回none
print(soup.a.get_text())

在这里插入图片描述

3.对获取的属性进行修改

print(soup.a.get(‘href’))
soup.a[‘href’] = ‘http://www.baidu.com
print(soup.a.get(‘href’))
print(soup.a)

在这里插入图片描述

面向对象的匹配

1.查找符合条件的所有标签

aTagObj=soup.find_all(‘a’)
print(aTagObj)
for item in aTagObj:

print(item)

在这里插入图片描述

2.需求:获取所有a标签,类名为sister

aTagObj=soup.find_all(‘a’,class_=‘sister’)
print(aTagObj)

在这里插入图片描述

3.根据内容进行匹配

print(soup.find_all(text=“story”))
print(soup.find_all(text=re.compile(‘story\d+’)))
在这里插入图片描述

BS4模块的补充

soup=BeautifulSoup(html,‘lxml’)
html与xml的区别
XML 不是 HTML 的替代。
XML 和 HTML 为不同的目的而设计:
XML 被设计为传输和存储数据,其焦点是数据的内容。
HTML 被设计用来显示数据,其焦点是数据的外观。
HTML 旨在显示信息,而 XML 旨在传输信息。

1.返回符合条件的第一个标签

print(soup.title)
print(soup.p)
print(soup.find(‘p’, class_=re.compile(r’^ti.*?’)))

2.返回符合条件的所有标签

print(soup.find_all(‘p’))
print(soup.find_all(‘p’, class_=‘title’, text=re.compile(r’.?story.?’)))

3.获取符合条件的p标签或者a标签

print(soup.find([‘title’, ‘a’]))
print(soup.find_all([‘title’, ‘a’]))
print(soup.find_all([‘title’, ‘a’], class_=[‘title’, ‘sister’]))

  1. CSS匹配
    不支持正则表达式,且格式与css一致

#标签选择器
print(soup.select(“title”))
#类选择器(.类名)
print(soup.select(".sister"))
#id选择器(#id名称)
print(soup.select("#link1"))
#属性选择器()
print(soup.select(“input[type=‘password’]”))

在这里插入图片描述

bs4模块的解释器

1.Python 标准库:
BeautifulSoup(html,‘html.parser’)
Python的内置标准库
执行速度适中
文档容错能力强(python3.2.2前容错能力差)

2.lxml(HTML解析器)
BeautifulSoup(html,‘lxml’)
速度快
唯一支持XML的解析器
需要安装C语言库

3.lxml(XML解析器)
BeautifulSoup(html,[“lxml-xml”])
BeautifulSoup(html,‘xml’)
速度快
唯一支持XML的解析器
需要安装C语言库

3.html5lib
BeautifulSoup(html,‘html5lib’)
最好的容错性
以浏览器的方式解析文档
生成HTML5格式的文档
速度慢
不依赖外部拓展

猜你喜欢

转载自blog.csdn.net/qq_37037438/article/details/87791368