擅长:python、mysql、java
<p>我不会检索整个集合,而是使用<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type" rel="nofollow noreferrer">:nth-of-type</a>对选择器本身中的前4个同级p标记进行更有效的筛选:</p>
<pre><code>import requests
from bs4 import BeautifulSoup as bs
from pprint import pprint
r = requests.get('https://www.adobe.com/support/security/advisories/apsa11-04.html')
soup = bs(r.content, 'html.parser')
pprint([i.text for i in soup.select('h2 ~ p:nth-of-type(-n+4)')])
</code></pre>
<p>您还可以使用<a href="https://beautiful-soup-4.readthedocs.io/en/latest/#the-limit-argument" rel="nofollow noreferrer">limit argument</a>:</p>
<pre><code>pprint([i.text for i in soup.select('h2 ~ p', limit = 4)])
</code></pre>