【问题标题】:Get content of a header tag searching by header tag name通过标题标签名称搜索标题标签的内容
【发布时间】:2015-11-29 23:10:55
【问题描述】:

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

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

我需要得到数字“20”问题是这个数字并不总是在同一个标​​题中,有时在“h4”中并且有更多的“h5”标题,所以我需要找到数据包含在名为“员工人数”的标题中,并提取包含段落中的数字

这是页面的链接

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

【问题讨论】:

    标签: python xpath beautifulsoup scrapy web-scripting


    【解决方案1】:

    嗯,最简单的方法是找到一个包含“员工人数”文本的元素,然后简单地取其后的段落,假设该段落总是紧随其后。

    这是一个快速而肮脏的代码,它可以执行此操作,并将数字打印出来:

    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())
    

    【讨论】:

      【解决方案2】:
      'normalize-space(//*[self::h4 or self::h5][contains(., "Number of Employees")]/following-sibling::p[1]/text())'
      

      【讨论】:

      • 那是 Xpath 吗?我试了一下,得到了很长的回复,而不仅仅是数字
      • 对不起,我只是给你一个想法,请检查更正后的 xpath
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2014-12-17
      • 2014-07-11
      • 2014-01-05
      • 1970-01-01
      相关资源
      最近更新 更多