擅长:python、mysql、java
<p>您需要的所有段落都位于<code><div class="td-post-content"></code>标记内,以及作者信息的段落。但是,必需的<code><p></code>标记是此<code><div></code>标记的直接子标记,而其他不需要的{<cd2>}标记不是直接子标记(它们嵌套在其他<code>div</code>标记中)。在</p>
<p>因此,您只能使用<a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-recursive-argument" rel="nofollow noreferrer"><strong>^{<cd6>}</strong></a>来访问这些标记。在</p>
<p><strong>代码:</strong></p>
<pre><code>import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
r = requests.get('https://www.the-blockchain.com/2018/06/29/mcafee-labs-report-6x-increase-in-crypto-mining-malware-incidents-in-q1-2018/', headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
container = soup.find('div', class_='td-post-content')
for para in container.find_all('p', recursive=False):
print(para.text)
</code></pre>
<p><strong>输出:</strong></p>
^{pr2}$