html - BeautifulSoup 匹配错误的类
问题描述
我正在使用如下所示的 HTML:
<td class="hidden-xs BuildingUnit-price" data-sort-value="625000">
<span class="price">$625,000 </span>
</td>
<td class="hidden-xs BuildingUnit-bedrooms" data-sort-value="4.0">
4 rooms, 2 beds
</td>
<td class="hidden-xs BuildingUnit-bathrooms">
5 baths
</td>
<td class="hidden-xs" data-sort-value="1">
1 bath
</td>
我编写了下面的脚本来识别具有“hidden-xs”类的 td 标签,以便为房地产列表提取浴室数量,但它也与“hidden-xs BuildingUnit-price”类匹配。我该如何纠正?
#Extract the number of baths
import re
lst_baths=list()
baths=soup.find_all("td", class_=["hidden-xs"])
bath_lines=[td.get_text().strip() for td in baths]
pattern=re.compile(r'(\d{1})\D*(bath|baths)$')
for bath in bath_lines:
match=pattern.match(bath)
if match:
lst_baths.append(bath.split()[0])
例如,正如它目前所写的那样,我的代码选择了“5 个浴室”这一行,但我只希望它选择“1 个浴室”这一行。
解决方案
找到了一种方法来测试每个匹配的类:
#Extract the baths
lst_baths=list()
temp_lst=list()
baths=soup.find_all("td", class_=["hidden-xs"])
for item in baths:
if item['class']==['hidden-xs']:
temp_lst.append(item)
else:
pass
bath_lines=[td.get_text().strip() for td in temp_lst]
pattern=re.compile(r'(\d{1})\D*(bath|baths)$')
for bath in bath_lines:
match=pattern.match(bath)
if match:
lst_baths.append(bath.split()[0])
推荐阅读
- javascript - JSPDF and Html2Canvas how to reduce pdf file size
- python - 使用操作系统和系统在python中输入命令行
- memory - How to find the static adress/pointer for a game
- redirect - Nuxt.js 中 redirect() 和 req.writehead() 的区别
- bsd - 在 TwinCAT/BSD 中使用时更改 TF6250-Modbus-TCP 的配置
- php - 使用 CONCAT() 的 SELECT 语句和使用 CONCAT() 的 WHERE
- rust - 预期的不透明类型,找到枚举`Result`
- flutter - 我的应用程序显示空白的白色画布
- arrays - Rasterio 创建 TIFF 文件
- python - 在 tkinter 中将绘图添加到框架中