在包含多个结果的页面上抓取一个结果的信息

Scraping Information for One Result on a Page of Multiple Results

我想 scrape/parse 从包含多个结果的页面中获取特定结果的数据。

例如,下面是一个页面的来源 html 的剪辑,该页面在商业目录中有两个商业搜索结果。两者都有诸如 Status 之类的业务项。但是,我只想要与街道地址 311 South Swall Drive 关联的业务项目。

</section><section itemscope itemtype="http://schema.org/Organization" class="org">
<div class="b-business-item">
<div class='b-business-item_header-wrap  '>
<div class='b-business-item_title-wrap'>
<h2 class="b-business-item_header uppercase"><a itemprop="url" href="/p/kash+apparel+lp-12645872"><font itemprop="name">Kash Apparel, Lp</font></a></h2>

<p class="b-business-item_sub-header"><span class="addr-cont" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="streetAddress">2615 Fruitland Ave</span>, <span><span itemprop="addressLocality">Los Angeles</span>, <span itemprop="addressRegion">CA</span> <span itemprop="postalCode">90058</span></span></span></p>
</div>
</div>
<p class="b-business-item_props"><span class="b-business-item_title">Status:</span><span class="b-business-item_value">Inactive</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">Industry:</span><span class="b-business-item_value">Mfg Women's/Misses' Outerwear</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">Members (3):</span><span class="b-business-item_value">Mel Salde <span class='gray-text'>(Accountant, inactive)</span><br/>Edir Haroni <span class='gray-text'>(Limited Partner, inactive)</span><br/>Stephanie Kleinjan <span class='gray-text'>(General Partner, inactive)</span></span></p>
</div>
</section><section itemscope itemtype="http://schema.org/Organization" class="org">
<div class="b-business-item">
<div class='b-business-item_header-wrap  '>
<div class='b-business-item_title-wrap'>
<h2 class="b-business-item_header uppercase"><a itemprop="url" href="/p/kash+inc-178509132"><font itemprop="name">KASH INC</font></a></h2>

<p class="b-business-item_sub-header"><span class="addr-cont" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="streetAddress">311 South Swall Drive</span>, <span><span itemprop="addressLocality">Los Angeles</span>, <span itemprop="addressRegion">CA</span> <span itemprop="postalCode">90048</span></span></span></p>
</div>
</div>
<p class="b-business-item_props"><span class="b-business-item_title">Status:</span><span class="b-business-item_value">Inactive</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">Registration:</span><span class="b-business-item_value">Sep 26, 2006</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">State ID:</span><span class="b-business-item_value">C2904860</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">Business type:</span><span class="b-business-item_value">Articles of Incorporation</span></p>
<p class="b-business-item_props"><span class="b-business-item_title">Member:</span><span class="b-business-item_value">Ashwant Venkatram <span class='gray-text'>(President, inactive)</span></span></p>

我正在尝试抓取 311 South Swall Drive 的状态、注册、州 ID、业务类型和成员,而不是其他结果的类似字段。不幸的是,公司目录无法输入地址以将搜索范围缩小到一个结果。

我想这就是您要找的:

for sect in soup.find_all('section'):
  for adrs in sect.select('span[itemprop="streetAddress"]'):
    if adrs.text == '311 South Swall Drive':
        for item in sect.select('p'):
            print(item.text)

输出:

311 South Swall Drive, Los Angeles, CA 90048
Status:Inactive
Registration:Sep 26, 2006
State ID:C2904860
Business type:Articles of Incorporation
Member:Ashwant Venkatram (President, inactive)