为了账号安全,请及时绑定邮箱和手机立即绑定

使用美丽的汤在 HTML 表格中查找信息

使用美丽的汤在 HTML 表格中查找信息

温温酱 2021-10-05 15:58:31
我正在尝试从 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 类名。
查看完整描述

2 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

您可以遍历每一行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:'], []]



查看完整回答
反对 回复 2021-10-05
?
慕的地6264312

TA贡献1817条经验 获得超6个赞

例如,如果您知道您特别想查找字符串“Building style:”,那么您可以捕获.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 ]


查看完整回答
反对 回复 2021-10-05
  • 2 回答
  • 0 关注
  • 217 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号