python-bs4模块

0. 概括

  • 获取页面: urllib, requests
  • 解析页面信息: 正则表达式, BeautifulSoup4(BS4)

1. BS4简介

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

2. BS4的4种对象

2-1. BeautifulSoup对象

2-2. Tag对象

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

2-3. NavigableString

既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用.string即可

2-4. 评论

注释对象是一个特殊类型的NavigableString对象,其输出的内容不包括注释符号。

安装bs4:pip install bs4
下面根据这个例子来具体了解bs4

html = """
<html>
<head><title class='title'>story12345</title></head>
<body>
<p class="title" name="dromouse">The Dormouse's story</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>

<input type="text">
<input type="password">
"""

这段html代码不太标准可以用prettify修正

import re
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())

输出:

<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 class="sister" href="http://example.com/elsie" id="link1">
    <span>
     westos
    </span>
    <!-- Elsie -->
   </a>
   ,
   <a class="sister1" 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>
  <p class="story">
   ...
  </p>
 </body>
</html>

1. 根据标签获取内容;

# ******************标签的常用属性************************
# # 根据格式化, 如果title只有一个, 根据标签可以获取
print(soup.title)
print(type(soup.title))
print(soup.title.name)  # 标签的名称

# 获取标签里面的属性信息
print(soup.a.attrs)
print(soup.a.attrs['href'])

输出:

<title>story12345</title>
<class 'bs4.element.Tag'>
title
{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}
http://example.com/elsie

# *******************标签常用的方法*************************
#get方法用于得到标签下的属性值,注意这是一个重要的方法,在许多场合都能用到,比如你要得到<img src=”#”>标签下的图像url,那么就可以用soup.img.get(‘src’)
print(soup.a.get('href')) # get 只能获取每类标签的第一个标签
print(soup.a.get('class'))

# string得到标签下的文本内容,只有在此标签下没有子标签,或者只有一个子标签的情况下才能返回其中的内容,否则返回的是None;
# get_text()可以获得一个标签中的所有文本内容,包括子孙节点的内容,这是最常用的方法
print(soup.a.string)    # 也是只获取第一个标签,标签里面的内容 ,如果嵌套子标签比如这里嵌套span就不能获取到
print(soup.a.get_text())

输出:

http://example.com/elsie
['sister']
None
westos

# *******************对获取的属性信息进行修改***********************
print(soup.a.get('href'))
soup.a['href'] = 'http://www.baidu.com' # 修改a标签中的href
print(soup.a.get('href'))
print(soup.a)

输出:

http://example.com/elsie
http://www.baidu.com
<a class="sister" href="http://www.baidu.com" id="link1"><span>westos</span><!-- Elsie --></a>

2. 面向对象的匹配

# 查找符合条件的所有标签;
aTagObj =  soup.find_all('a')
print(aTagObj)
# 方便观察我们把它遍历出来
for item in aTagObj:
    print(item)

输出:

[<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister1" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>
<a class="sister1" href="http://example.com/lacie" id="link2">Lacie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

需求: 获取所有的a标签, 并且类名为"sister"

aTagObj = soup.find_all('a', class_="sister")
print(aTagObj)

输出:

[<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

修改下html的头部信息,改成story
在这里插入图片描述

输出匹配的字符

print(soup.find_all(text="story"))

输出:

['story']

也可以用正则表达式,修改成story1234
在这里插入图片描述

print(soup.find_all(text="story"))
# 也可以加入按照正则匹配
print(soup.find_all(text=re.compile('story\d+')))

输出:

[]
['story1234']

补充:
官方中文文档: https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
下表列出了主要的解析器,以及它们的优缺点:

Python标准库 HTML 解析器 HTML 解析器 html5lib
BeautifulSoup(markup, “html.parser”)Python的内置标准库执行速度适中文档容错能力强Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差 BeautifulSoup(markup, “html.parser”)Python的内置标准库执行速度适中文档容错能力强Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差 BeautifulSoup(markup, “lxml”)速度快文档容错能力强需要安装C语言库lxml BeautifulSoup(markup, “html5lib”)最好的容错性以浏览器的方式解析文档生成HTML5格式的文档速度慢不依赖外部扩展
import re

from bs4 import BeautifulSoup


html = """
<html>
<head><title class='title'>story12345</title></head>
<body>
<p class="title" name="dromouse">The Dormouse's story</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>

<input type="text">
<input type="password">
"""
soup = BeautifulSoup(html, 'html.parser')
# 需要安装第三方模块lxml解析器;
# soup = BeautifulSoup(html, 'lxml')

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

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

输出:

<title class="title">story12345</title>
<p class="title" name="dromouse">The Dormouse's story</p>
<p class="title" name="dromouse">The Dormouse's story</p>

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


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

输出:

[<p class="title" name="dromouse">The Dormouse's story</p>, <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"><span>westos</span><!-- Elsie --></a>,
<a class="sister1" 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>, <p class="story">...</p>]

[<p class="title" name="dromouse">The Dormouse's story</p>]

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

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

输出

<title class="title">story12345</title>

[<title class="title">story12345</title>, <a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister1" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

[<title class="title">story12345</title>, <a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

4. CSS匹配

# 标签选择器
print(soup.select("title"))
# 类选择器(.类名)
print(soup.select(".sister"))
# id选择器(#id名称)
print(soup.select("#link1"))
# 此处不支持正则表达式;
# print(soup.select(re.compile("#link\d+")))
# 属性选择器()
print(soup.select("input[type='password']"))

输出:

[<title class="title">story12345</title>]
[<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>]
[<input type="password"/>]

猜你喜欢

转载自blog.csdn.net/weixin_43067754/article/details/87778256
今日推荐