使用beautifulsoup提取没有类或id的元素

时间:2018-10-20 12:19:51

标签: python html web-scraping beautifulsoup

我有一个包含以下数据的HTML页面:

    <table cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td style="width:50%;padding-right:8px;" valign="top">
      <h2 class="sectionTitle">Corporate Headquarters</h2>
      <div itemprop="workLocation">6901 Professional Parkway East<br />Sarasota, Florida 34240<br /><br />United States<br /><br /></div><span class="detail">Phone</span>: <span itemprop="telephone">941-556-2601</span><br /><span class="detail">Fax</span>: <span itemprop="faxNumber">--</span>
      <h2 class="sectionTitle">Board Members Memberships</h2>
      <div>2011-Present</div><div><strong>Director</strong></div><div style="padding-bottom:15px;"><a href="../../stocks/snapshot/snapshot.asp?capId=11777224">TrustWave Holdings, Inc.</a></div><div>2018-Present</div><div><strong>President, CEO & Director</strong></div><div style="padding-bottom:15px;"><a href="../../stocks/snapshot/snapshot.asp?capId=22751">Roper Technologies, Inc.</a></div>
      <h2 class="sectionTitle">Education</h2>
      <div><strong>MBA</strong>  </div><div style="padding-bottom:15px;" itemprop="alumniOf">Harvard Business School</div><div><strong>Unknown/Other Education</strong>  </div><div style="padding-bottom:15px;" itemprop="alumniOf">Miami University</div><div><strong>Bachelor's Degree</strong>  </div><div style="padding-bottom:15px;" itemprop="alumniOf">Miami University</div>
      <h2 class="sectionTitle">Other Affiliations</h2>
      <div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=424885">MedAssets, Inc.</a></div><div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=1131022">Harvard Business School</a></div><div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=4109057">Miami University</a></div><div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=6296385">MedAssets Net Revenue Systems, LLC</a></div><div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=11777224">TrustWave Holdings, Inc.</a></div><div><a itemprop="affiliation" href="../../stocks/snapshot/snapshot.asp?capId=138296355">Medassets Services LLC</a></div>
    </td>

我正在尝试将有关“董事会成员资格”的信息提取为

Director 
TrustWave Holdings, Inc.


CEO & Director 
Roper Technologies, Inc.

这些没有任何类或id以便于提取。

但是,我能做的只是:

soup.find('td',style="width:50%;padding-right:8px;").findAll("strong")

这给了我以下结果:

[<strong>Director</strong>,
 <strong>President, CEO &amp; Director</strong>,
 <strong>MBA</strong>,
 <strong>Unknown/Other Education</strong>,
 <strong>Bachelor's Degree</strong>]

有人可以指导我怎么做吗?

2 个答案:

答案 0 :(得分:1)

我的Python技能有点生锈,所以我将以伪代码为您提供答案,助您一臂之力。祝你好运!

result = ""

tdContent = soup.find('td',style="width:50%;padding-right:8px;")
headers = tdContent.findAll("h2")
for header in headers:
    if header.text == "Board Members Memberships":
        for (item = header; item.name != "h2"; item = item.nextSibling):
            if item.hasChild(strong):
                result += item.getChild(strong).getText + END_LINE
            if item.hasChild(a):
                result += item.getChild(a).getText + END_LINE

答案 1 :(得分:1)

您可以使用BeautifulSoup中提供的导航选项。几个循环和一些条件语句将帮助您实现所需的功能。

第1步:遍历所有div标签。

步骤2:通过检查div是否包含strong来过滤位置。

第3步:导航到父对象的下一个div标签,即具有位置的div

第4步:使用公司名称包装在a标记中的条件过滤兄弟姐妹。

titles = soup.find_all('div')
for title in titles:
    if title.strong:
        company = title.find_next_sibling('div')
        if company.a:
            person_title = title.text
            person_company = company.text
            print(person_company, person_title)

希望这会有所帮助!干杯!

相关问题