【发布时间】:2020-02-03 10:16:46
【问题描述】:
我想为一堆下载的文件提取 P 强元素之间的文本。 我想要 P strong “Executives” 和 P strong “Analysts” 之间的所有 P 文本,我附上了一个 html 示例,请参阅example 我知道如何加载 html,但我不知道如何使用 BS4 提取前面提到的数据:
import textwrap
import os
from bs4 import BeautifulSoup
directory ='C:/test/out'
for filename in os.listdir(directory):
if filename.endswith('.html'):
fname = os.path.join(directory,filename)
with open(fname, 'r') as f:
soup = BeautifulSoup(f.read(),'html.parser')
html 示例:
</header><div id="a-cont"><div class="p p1"></div><div class="sa-art article-width" id="a-body"><p>Apple, Inc. (NASDAQ:<a href="https://seekingalpha.com/symbol/AAPL" title="Apple Inc.">AAPL</a>)</p>
<p>Q4 2016 Earnings Call</p>
<p>October 25, 2016 5:00 pm ET</p>
<p><strong>Executives</strong></p>
<p>Nancy Paxton - Apple, Inc.</p>
<p>Timothy Donald Cook - Apple, Inc.</p>
<p>Luca Maestri - Apple, Inc.</p>
<p><strong>Analysts</strong></p>
<p>Eugene Charles Munster - Piper Jaffray & Co.</p>
<p>Kathryn Lynn Huberty - Morgan Stanley & Co. LLC</p>
<p>Shannon S. Cross - Cross Research LLC</p>
<p>Antonio M. Sacconaghi - Sanford C. Bernstein & Co. LLC</p>
<p>Simona K. Jankowski - Goldman Sachs & Co.</p>
<p>Steven M. Milunovich - UBS Securities LLC</p>
<p>Wamsi Mohan - Bank of America Merrill Lynch</p>
<p>James D. Suva - Citigroup Global Markets, Inc. (Broker)</p>
<p>Rod B. Hall - JPMorgan Securities LLC</p>
【问题讨论】:
-
欢迎来到 SO。你能分享这个“html.parser”文件的示例数据吗?
-
谢谢@Sampath!我用 html 代码更新了我的问题。
标签: python html python-3.x web-scraping beautifulsoup