Beautiful Soup 找出文档中的所有< p>标签

Beautiful Soup 找出文档中的所有<p>标签

有很多BeautifulSoup的方法,可以帮助我们搜索解析树。最常用的两种方法是find()和find_all()。

在讲解find()和find_all()之前,让我们先看一些不同过滤器的示例,可以传递给这些方法。

过滤器种类

我们有不同的过滤器可以传递给这些方法,并且了解这些过滤器的重要性是至关重要的,因为这些过滤器将在整个搜索API中反复使用。我们可以根据标签的名称、属性、字符串的文本或这些的混合使用这些过滤器。

字符串

最简单类型的过滤器之一是字符串。将字符串传递给搜索方法,Beautifulsoup将与该字符串进行匹配。

下面的代码将查找文档中的所有<p>标签−

>>> markup = BeautifulSoup('<p>Top Three</p><p><pre>Programming Languages are:</pre></p><p><b>Java, Python, Cplusplus</b></p>')
>>> markup.find_all('p')
[<p>Top Three</p>, <p></p>, <p><b>Java, Python, Cplusplus</b></p>]

正则表达式

您可以查找以给定字符串/标签开头的所有标签。在此之前,我们需要导入re模块以使用正则表达式。

>>> import re
>>> markup = BeautifulSoup('<p>Top Three</p><p><pre>Programming Languages are:</pre></p><p><b>Java, Python, Cplusplus</b></p>')
>>>
>>> markup.find_all(re.compile('^p'))
[<p>Top Three</p>, <p></p>, <pre>Programming Languages are:</pre>, <p><b>Java, Python, Cplusplus</b></p>]

列表

您可以通过提供一个列表来传递多个标签进行查找。以下代码查找所有的<b><pre>标签:

>>> markup.find_all(['pre', 'b'])
[<pre>Programming Languages are:</pre>, <b>Java, Python, Cplusplus</b>]

True

True会返回它能找到的所有标签,但不会返回任何单独的字符串−

>>> markup.find_all(True)
[<html><body><p>Top Three</p><p></p><pre>Programming Languages are:</pre>
<p><b>Java, Python, Cplusplus</b> </p> </body></html>, 
<body><p>Top Three</p><p></p><pre> Programming Languages are:</pre><p><b>Java, Python, Cplusplus</b></p>
</body>, 
<p>Top Three</p>, <p></p>, <pre>Programming Languages are:</pre>, <p><b>Java, Python, Cplusplus</b></p>, <b>Java, Python, Cplusplus</b>]

仅从上面的soup返回标签 –

>>> for tag in markup.find_all(True):
(tag.name)
'html'
'body'
'p'
'p'
'pre'
'p'
'b'

find_all()

使用find_all()可以从页面响应中提取特定标签的所有出现-

语法

find_all(name, attrs, recursive, string, limit, **kwargs)

让我们从IMDB中提取一些有趣的数据-“有史以来最受好评的电影”。

>>> url="https://www.imdb.com/chart/top/?ref_=nv_mv_250"
>>> content = requests.get(url)
>>> soup = BeautifulSoup(content.text, 'html.parser')
#Extract title Page
>>> print(soup.find('title'))
<title>IMDb Top 250 - IMDb</title>

#Extracting main heading
>>> for heading in soup.find_all('h1'):
   print(heading.text)
Top Rated Movies

#Extracting sub-heading
>>> for heading in soup.find_all('h3'):
   print(heading.text)

IMDb Charts
You Have Seen
   IMDb Charts
   Top India Charts
Top Rated Movies by Genre
Recently Viewed

从上面,我们可以看到find_all会给我们返回所有符合我们定义的搜索条件的项目。find_all()可以和find()以及其他搜索方法一起使用的所有过滤器,也可以用于find_parents()或find_siblings()。

find()

如上所述,find_all()用于扫描整个文档以找到所有内容,但有时候我们只需要找到一个结果。如果您知道文档中只有一个标签,那么搜索整个文档是浪费时间。一种方法是每次调用find_all()时添加limit=1,或者可以使用find()方法来实现相同的功能-

语法

find(name, attrs, recursive, string, **kwargs)

所以下面的两种不同方法得到相同的输出 –

>>> soup.find_all('title',limit=1)
[<title>IMDb Top 250 - IMDb</title>]
>>>
>>> soup.find('title')
<title>IMDb Top 250 - IMDb</title>

在上述输出中,我们可以看到find_all()方法返回一个包含单个项的列表,而find()方法返回单个结果。

find()和find_all()方法之间的另一个区别是−

>>> soup.find_all('h2')
[]
>>>
>>> soup.find('h2')

如果soup.find_all()方法找不到任何内容,它会返回空列表,而find()方法返回None。

find_parents()和find_parent()

与遍历树查找标签的子元素的find_all()和find()方法不同,find_parents()和find_parents methods()方法相反,它们向上遍历树并查找标签(或字符串)的父元素。

语法

find_parents(name, attrs, string, limit, **kwargs)
find_parent(name, attrs, string, **kwargs)

>>> a_string = soup.find(string="The Godfather")
>>> a_string
'The Godfather'
>>> a_string.find_parents('a')
[<a href="/title/tt0068646/" title="Francis Ford Coppola (dir.), Marlon Brando, Al Pacino">The Godfather</a>]
>>> a_string.find_parent('a')
<a href="/title/tt0068646/" title="Francis Ford Coppola (dir.), Marlon Brando, Al Pacino">The Godfather</a>
>>> a_string.find_parent('tr')
<tr>

<td class="posterColumn">
<span data-value="2" name="rk"></span>
<span data-value="9.149038526210072" name="ir"></span>
<span data-value="6.93792E10" name="us"></span>
<span data-value="1485540" name="nv"></span>
<span data-value="-1.850961473789928" name="ur"></span>
<a href="/title/tt0068646/"> <img alt="The Godfather" height="67" src="https://m.media-amazon.com/images/M/MV5BM2MyNjYxNmUtYTAwNi00MTYxLWJmNWYtYzZlODY3ZTk3OTFlXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_UY67_CR1,0,45,67_AL_.jpg" width="45"/>
</a> </td>
<td class="titleColumn">
2.
<a href="/title/tt0068646/" title="Francis Ford Coppola (dir.), Marlon Brando, Al Pacino">The Godfather</a>
<span class="secondaryInfo">(1972)</span>
</td>
<td class="ratingColumn imdbRating">
<strong title="9.1 based on 1,485,540 user ratings">9.1</strong>
</td>
<td class="ratingColumn">
<div class="seen-widget seen-widget-tt0068646 pending" data-titleid="tt0068646">
<div class="boundary">
<div class="popover">
<span class="delete"> </span><ol><li>1<li>2<li>3<li>4<li>5<li>6<li>7<li>8<li>9<li>10</li>0</li></li></li></li&td</li></li></li></li></li></ol> </div>
</div>
<div class="inline">
<div class="pending"></div>
<div class="unseeable">NOT YET RELEASED</div>
<div class="unseen"> </div>
<div class="rating"></div>
<div class="seen">Seen</div>
</div>
</div>
</td>
<td class="watchlistColumn">

<div class="wlb_ribbon" data-recordmetrics="true" data-tconst="tt0068646"></div>
</td>
</tr>
>>>
>>> a_string.find_parents('td')
[<td class="titleColumn">
2.
<a href="/title/tt0068646/" title="Francis Ford Coppola (dir.), Marlon Brando, Al Pacino">The Godfather</a>
<span class="secondaryInfo">(1972)</span>
</td>]

还有八种其他类似的方法−

find_next_siblings(name, attrs, string, limit, **kwargs)
find_next_sibling(name, attrs, string, **kwargs)

find_previous_siblings(name, attrs, string, limit, **kwargs)
find_previous_sibling(name, attrs, string, **kwargs)

find_all_next(name, attrs, string, limit, **kwargs)
find_next(name, attrs, string, **kwargs)

find_all_previous(name, attrs, string, limit, **kwargs)
find_previous(name, attrs, string, **kwargs)

在这里,

find_next_siblings()find_next_sibling() 方法将迭代当前元素之后的所有兄弟元素。

find_previous_siblings()find_previous_sibling() 方法将迭代当前元素之前的所有兄弟元素。

find_all_next()find_next() 方法将迭代当前元素之后的所有标签和字符串。

find_all_previousfind_previous() 方法将迭代当前元素之前的所有标签和字符串。

CSS选择器

The BeautifulSoup库支持最常用的CSS选择器。你可以使用select()方法通过CSS选择器搜索元素。

以下是一些示例 –

>>> soup.select('title')
[<title>IMDb Top 250 - IMDb</title>, <title>IMDb Top Rated Movies</title>]
>>>
>>> soup.select("p:nth-of-type(1)")
[<p>The Top Rated Movie list only includes theatrical features.</p>, <p> class="imdb-footer__copyright _2-iNNCFskmr4l2OFN2DRsf">© 1990-2019 by IMDb.com, Inc.</p>]
>>> len(soup.select("p:nth-of-type(1)"))
2
>>> len(soup.select("a"))
609
>>> len(soup.select("p"))
2

>>> soup.select("html head title")
[<title>IMDb Top 250 - IMDb</title>, <title>IMDb Top Rated Movies</title>]
>>> soup.select("head > title")
[<title>IMDb Top 250 - IMDb</title>]

#print HTML code of the tenth li elemnet
>>> soup.select("li:nth-of-type(10)")
[<li class="subnav_item_main">
<a href="/search/title?genres=film_noir&sort=user_rating,desc&title_type=feature&num_votes=25000,">Film-Noir
</a> </li>]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程