你的beautifulsoup pull 太具体了。您正在捕获所有“跨度”标签,其中 class= 值。
当您查看 HTML 时,您可以通过搜索某些字段的文本来快速找到该部分。您应该做的是获取 class= 'infoEntity' 的任何 div 标签内的所有内容,其中包含您有兴趣从“概述”部分获取的所有 7 个字段。
在其中,每个字段都有一个标签标签,它具有与您想要的标签相关的属性,并且在概述部分中。
所以,从以下开始:
from bs4 import BeautifulSoup
data = """
<div class="eep-pill"><p class="tightVert h2 white"><strong>Enhanced</strong> Profile <span class="round ib"><i class="icon-star-white"></i></span></p></div></header><section class="center flex-grid padVertLg eepModal"><h2>Try Enhanced Profile Free for a Month</h2><p>Explore the many benefits of having a premium branded profile on Glassdoor, like increased influence and advanced analytics.</p><div class="margBot"><i class="feaIllustration"></i></div><a href='/employers/enhanced/landing_input.htm?src=info_mod' class='gd-btn gd-btn-link gradient gd-btn-1 gd-btn-med span-1-2'><span>Get Started</span><i class='hlpr'></i></a><p>Changes wont be saved until you sign up for an Enhanced Profile subscription.</p></section></div></article><article id='MainCol'><div id='EmpBasicInfo' class='module empBasicInfo ' data-emp-id='1138'><div class=''><header class='tbl fill '><h2 class='cell middle tightVert blockMob'> Apple Overview</h2></header><div class='info flexbox row col-hh'><div class='infoEntity'><label>Website</label><span class='value website'><a class="link" href="http://www.apple.com" target="_blank" rel="nofollow noreferrer">www.apple.com</a></span></div><div class='infoEntity'><label>Headquarters</label><span class='value'>Cupertino, CA</span></div><div class='infoEntity'><label>Size</label><span class='value'>10000+ employees</span></div><div class='infoEntity'><label>Founded</label><span class='value'> 1976</span></div><div class='infoEntity'><label>Type</label><span class='value'> Company - Public (AAPL) </span></div><div class='infoEntity'><label>Industry</label><span class='value'> Computer Hardware & Software</span></div><div class='infoEntity'><label>Revenue</label><span class='value'> $10+ billion (USD) per year</span></div></div></div><div class=''><div data-full="We&rsquo;re a diverse collection of thinkers and doers, continually reimagining what&rsquo;s possible to help us all do what we love in new ways. The people who work here have reinvented entire industries with the Mac, iPhone, iPad, and Apple Watch, as well as with services, including iTunes, the App Store, Apple Music, and Apple Pay. And the same passion for innovation that goes into our products also applies to our practices &mdash; strengthening our commitment to leave the world better than we found it." class='margTop empDescription'> We’re a diverse collection of thinkers and doers, continually reimagining what’s possible to help us all do what we love in new ways. The people who work here have reinvented entire industries with the Mac, iPhone, iPad, and Apple Watch, as well as with ... <span class='link minor moreLink' id='ExpandDesc'>Read more</span></div><div class='hr'><hr/></div><h3 class='margTop'>Glassdoor Awards</h3>
"""
items = []
soup = BeautifulSoup(data, 'lxml')
get_info = iter(soup.find_all("div", {"class" : "infoEntity"}))
for item in get_info:
label = item.find("label")
value = item.find("span")
items.append((label.string, value.string))
这样,您将获得项目中的元组列表,打印为:
[('Website', 'www.apple.com'), ('Headquarters', 'Cupertino, CA'), ('Size', '10000+ employees'), ('Founded', ' 1976'), ('Type', ' Company - Public (AAPL) '), ('Industry', ' Computer Hardware & Software'), ('Revenue', ' $10+ billion (USD) per year')]
您可以从那里以您喜欢的任何格式打印该列表。