我正在尝试提取两个元素“执行人员”和“分析师”之间的数据,例如,但是我不知道如何进行。我的html是:
<div class="content_part hid" id="article_participants">
<p>Wabash National Corporation (NYSE:<a title="" href="http://seekingalpha.com/symbol/wnc">WNC</a>)</p><p>Q4 2014 <span class="transcript-search-span" style="background-color: yellow;">Earnings</span> Conference <span class="transcript-search-span" style="background-color: rgb(243, 134, 134);">Call</span></p><p>February 04, 2015 10:00 AM ET</p>
<p><strong>Executives</strong></p>
<p>Mike Pettit - Vice President of Finance and Investor Relations</p>
<p>Richard Giromini - President and Chief Executive Officer</p>
<p>Jeffery Taylor - Senior Vice President and Chief Financial Officer</p>
<p><strong>Analysts</strong></p>
我想对一大堆文件执行此操作,直到现在为止:
from bs4 import BeautifulSoup
import requests
import textwrap
import os
from lxml import html
import csv
directory ='C:/Research syntheses - Meta analysis/SeekingAlpha'
for filename in os.listdir(directory):
if filename.endswith('.html'):
fname = os.path.join(directory,filename)
with open(fname, 'r') as f:
page=f.read()
soup = BeautifulSoup(f.read(),'html.parser')
match = soup.find('div',class_='content_part hid', id='article_participants')
print(match)
我是Python的新生,所以请耐心等待。
标题可以在以下HTML中找到:
<div class="page_header_email_alerts" id="page_header">
<h1>
<span itemprop="headline">Wabash National's (WNC) CEO Richard Giromini on Q4 2014 Results - Earnings Call Transcript</span>
</h1>
<div id="article_info">
<div class="article_info_pos">
<span itemprop="datePublished" content="2015-02-04T21:48:03Z">Feb. 4, 2015 4:48 PM ET</span>
<span id="title_article_comments"></span>
<span class="print_hide"><span class="print_hide"> | </span> <span>About:</span> <span id="about_primary_stocks"><a title="Wabash National Corporation" href="/symbol/WNC" sasource="article_primary_about_trc">Wabash National Corporation (WNC)</a></span></span>
<span class="author_name_for_print">by: SA Transcripts</span>
<span id="second_line_wrapper"></span>
</div>
'''
合并您的代码。
import os
from simplified_scrapy.simplified_doc import SimplifiedDoc
directory ='C:/Research syntheses - Meta analysis/SeekingAlpha'
for filename in os.listdir(directory):
if filename.endswith('.html'):
fname = os.path.join(directory,filename)
with open(fname, 'r') as f:
page=f.read()
doc = SimplifiedDoc(page)
headline = doc.select('div#article_info>span#about_primary_stocks>a>text()')
div = doc.select('div#article_participants')
if not div: continue
ps = div.getElements('p',start='<strong>Executives</strong>',end='<strong>Analysts</strong>')
Executives = [p.text.split('-')[0].strip() for p in ps]
ps = div.getElements('p',start='<strong>Analysts</strong>')
Analysts = [p.text.split('-')[0].strip() for p in ps]
print (headline)
print (Executives)
print (Analysts)
以下代码是一个示例。
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = '''
<div class="page_header_email_alerts" id="page_header">
<h1>
<span itemprop="headline">Wabash National's (WNC) CEO Richard Giromini on Q4 2014 Results - Earnings Call Transcript</span>
</h1>
<div id="article_info">
<div class="article_info_pos">
<span itemprop="datePublished" content="2015-02-04T21:48:03Z">Feb. 4, 2015 4:48 PM ET</span>
<span id="title_article_comments"></span>
<span class="print_hide"><span class="print_hide"> | </span> <span>About:</span> <span id="about_primary_stocks"><a title="Wabash National Corporation" href="/symbol/WNC" sasource="article_primary_about_trc">Wabash National Corporation (WNC)</a></span></span>
<span class="author_name_for_print">by: SA Transcripts</span>
<span id="second_line_wrapper"></span>
</div>
</div>
</div>
<div class="content_part hid" id="article_participants">
<p>Wabash National Corporation (NYSE:<a title="" href="http://seekingalpha.com/symbol/wnc">WNC</a>)</p><p>Q4 2014 <span class="transcript-search-span" style="background-color: yellow;">Earnings</span> Conference <span class="transcript-search-span" style="background-color: rgb(243, 134, 134);">Call</span></p><p>February 04, 2015 10:00 AM ET</p>
<p><strong>Executives</strong></p>
<p>Mike Pettit - Vice President of Finance and Investor Relations</p>
<p>Richard Giromini - President and Chief Executive Officer</p>
<p>Jeffery Taylor - Senior Vice President and Chief Financial Officer</p>
<p><strong>Analysts</strong></p>
<p>Jeffery Taylor - Senior Vice President and Chief Financial Officer</p>
</div>
'''
doc = SimplifiedDoc(html)
headline = doc.select('div#article_info>span#about_primary_stocks>a>text()')
div = doc.select('div#article_participants')
ps = div.getElements('p',start='<strong>Executives</strong>',end='<strong>Analysts</strong>')
Executives = [p.text.split('-')[0].strip() for p in ps]
ps = div.getElements('p',start='<strong>Analysts</strong>')
Analysts = [p.text.split('-')[0].strip() for p in ps]
print (headline)
print (Executives)
print (Analysts)
结果:
Wabash National Corporation (WNC)
[u'Mike Pettit', u'Richard Giromini', u'Jeffery Taylor']
[u'Jeffery Taylor']
这里有更多示例:https : //github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句