通过 header 标签名称搜索获取 header 标签的内容

Get content of a header tag searching by header tag name

我正在抓取一个页面,我必须从这种格式中获取员工人数:

<h5>Number of Employees</h5>
<p>
            20
</p>

我需要得到数字“20”,问题是这个数字并不总是相同的 header,有时在 "h4" 中并且有更多的“h5”headers,所以我需要找到包含在名为 header 中的数据:"Number of Employees" 并提取包含的段落中的数字

这是页面link

http://www.bbb.org/chicago/business-reviews/paving-contractors/lester-s-material-service-inc-in-grayslake-il-72000434/

嗯,最简单的方法是找到一个包含 "Number of Employees"-text 的元素,然后简单地取其后的段落,假设该段落总是紧跟在后面。

这是执行此操作并打印出数字的一段快速但肮脏的代码:

parent = soup.find("div", id='business-additional-info-text')
for child in parent.children:
    if("Number of Employees" in child):
        print(child.findNext('p').contents[0].strip())
'normalize-space(//*[self::h4 or self::h5][contains(., "Number of Employees")]/following-sibling::p[1]/text())'