python - 使用 beautifulsoup 提取没有类或 id 的元素
问题描述
我有一个包含以下数据的 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 & Director</strong>,
<strong>MBA</strong>,
<strong>Unknown/Other Education</strong>,
<strong>Bachelor's Degree</strong>]
有人可以指导我怎么做吗?
解决方案
您可以使用 BeautifulSoup 中提供的导航选项。几个循环和一些条件语句将帮助您实现您正在寻找的东西。
第 1 步:遍历所有div
标签。
div
第 2 步:通过检查是否包含来过滤职位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)
希望这可以帮助!干杯!
推荐阅读
- python - 相当于 Python getattr,但用于索引或键
- angular - Angular 9延迟加载模块的每个组件
- kotlin - Why sealed modifier cannot be used with object in Kotlin?
- javascript - fullcalendar.js v4 - 如何在列表视图的标题中设置 html?
- r - 如何更改axis.line.y的限制
- c++ - 在将数字转换为三进制基数时收集余数(C++)
- c++ - 如何使用 boost::spirit 解析两个字符串?
- kubernetes - K8S:使用 Istio 路由返回 404
- php - FOSUserBundle 不断将密码保存为明文
- python-3.x - Python 3 脚本占用过多内存