【问题标题】:Find information in HTML tables with Beautiful soup使用 Beautiful soup 在 HTML 表格中查找信息
【发布时间】:2019-06-02 12:27:16
【问题描述】:

我正在尝试从 html 表中提取信息(在此示例页面 https://www.detrasdelafachada.com/house-for-sale-marianao-havana-cuba/dcyktckvwjxhpl9 中找到):

<div class="row">
    <div class="col-label">
        Type of property:
    </div>
    <div class="col-datos">
        Apartment </div>
</div>
<div class="row">
    <div class="col-label">
        Building style:
    </div>
    <div class="col-datos">
        50 year </div>
</div>
<div class="row">
    <div class="col-label precio">
        Sale price:
    </div>
    <div class="col-datos precio">
        12 000 CUC </div>
</div>
<div class="row">
    <div class="col-label">
        Rooms:
    </div>
    <div class="col-datos">
        1 </div>
</div>
<div class="row">
    <div class="col-label">
        Bathrooms:
    </div>
    <div class="col-datos">
        1 </div>
</div>
<div class="row">
    <div class="col-label">
        Kitchens:
    </div>
    <div class="col-datos">
        1 </div>
</div>
<div class="row">
    <div class="col-label">
        Surface:
    </div>
    <div class="col-datos">
        38 mts2 </div>
</div>
<div class="row">
    <div class="col-label">
        Year of construction:
    </div>
    <div class="col-datos">
        1945 </div>
</div>
<div class="row">
    <div class="col-label">
        Building style:
    </div>
    <div class="col-datos">
        50 year </div>
</div>
<div class="row">
    <div class="col-label">
        Construction type:
    </div>
    <div class="col-datos">
        Masonry and plate </div>
</div>
<div class="row">
    <div class="col-label">
        Home conditions:
    </div>
    <div class="col-datos">
        Good </div>
</div>
<div class="row">
    <div class="col-label">
        Other peculiarities:
    </div>
</div>
<div class="row">

使用美丽的汤,我如何找到“建筑风格:”(以及其他条目)的价值?

我的问题是我直接找到类,因为表中的所有条目都具有相同的 div 类名。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    您可以遍历每一行 div 并找到嵌套的 div 值:

    from bs4 import BeautifulSoup as soup
    import re
    d = soup(content, 'html.parser')
    results = [[re.sub('\s{2,}|\n+', '', i.text) for i in b.find_all('div')] for b in d.find_all('div', {'class':'row'})]
    

    输出:

    [['Type of property:', 'Apartment '], ['Building style:', '50 year '], ['Sale price:', '12 000 CUC '], ['Rooms:', '1 '], ['Bathrooms:', '1 '], ['Kitchens:', '1 '], ['Surface:', '38 mts2 '], ['Year of construction:', '1945 '], ['Building style:', '50 year '], ['Construction type:', 'Masonry and plate '], ['Home conditions:', 'Good '], ['Other peculiarities:'], []]
    

    【讨论】:

      【解决方案2】:

      例如,如果您知道您特别想查找字符串“建筑风格:”,则可以捕获.next_sibling 的文本。或者直接使用next:

      >>> from bs4 import BeautifulSoup
      >>> html = "<c><div>hello</div> <div>hi</div></c>"
      >>> soup = BeautifulSoup(html, 'html.parser')
      >>> print(soup.find(string="hello").find_next('div').contents[0])
      hi
      

      如果你想要所有这些,你可以使用.find_all 来获取类“row”的所有 div 标签,然后获取每个标签的孩子。

      data = []
      soup = BeautifulSoup(html, 'html.parser')
      for row in soup.find_all('div', class_="row"):
          rowdata = [ c.text.strip() for c in row.find_all('div')]
          data.append(rowdata)
      print(data)
      # Outputs the nested list:
      #   [u'Type of property:', u'Apartment'], [u'Building style:', u'50 year'], etc ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 2020-11-25
        • 2015-07-19
        • 2015-11-08
        • 2017-05-31
        相关资源
        最近更新 更多