python - 查找未返回值的下一个兄弟姐妹。如何在没有其余类的情况下提取我需要的两个类?
问题描述
我想从下面的“内容”中提取物品重量和产品尺寸。我在这里想念什么?在我的脚本中,找不到我要查找的内容。有没有更简单的方法来提取物品重量和产品尺寸?谢谢
import bs4 as bs
content = '''
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Item Weight
</th>
<td class="a-size-base prodDetAttrValue">
0.16 ounces
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Product Dimensions
</th>
<td class="a-size-base prodDetAttrValue">
4.8 x 3.4 x 0.5 inches
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Batteries Included?
</th>
<td class="a-size-base prodDetAttrValue">
No
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Batteries Required?
</th>
<td class="a-size-base prodDetAttrValue">
No
</td>
</tr>
'''
soup = bs.BeautifulSoup(content, features='lxml')
try:
product = {
'weight': soup.find(text='Item Weight').parent.find_next_siblings(),
'dimension': soup.find(text='Product Dimensions').parent.find_next_siblings()
}
except:
product = {
'weight': 'item unavailable',
'dimension': 'item unavailable'
}
print(product)
追溯:
{'weight': 'item unavailable', 'dimension': 'item unavailable'}
解决方案
您错误地使用了查找下一个兄弟姐妹。td
标签是标签的兄弟而th
不是父tr
标签。
from bs4 import BeautifulSoup
import re
content = '''
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Item Weight
</th>
<td class="a-size-base prodDetAttrValue">
0.16 ounces
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Product Dimensions
</th>
<td class="a-size-base prodDetAttrValue">
4.8 x 3.4 x 0.5 inches
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Batteries Included?
</th>
<td class="a-size-base prodDetAttrValue">
No
</td>
</tr>
'''
soup = BeautifulSoup(content, 'html.parser')
d = {
'weight': soup.find('th', text=re.compile('\s*Item Weight\s*')).find_next_sibling('td').text.strip(),
'dimension': soup.find('th', text=re.compile('\s*Product Dimensions\s*')).find_next_sibling('td').text.strip()
}
print(d)
推荐阅读
- mongodb - 如何在单个请求中合并 3 个 mongoDB 集合或首先进行单个集合以进行电子商务?
- java - Watch Service API 是否可以识别内存映射文件?
- flutter - Flutter - 如何测试本地化的小部件?
- php - PHP 在 for 循环中使用引用
- javascript - React 无法识别 DOM 元素上的 `justifyContent` 属性
- python - Google Colab 使用 File-> Print 保存为 PDF 时出错
- python - 将字符串转换为类型的字典
- javascript - Node JS 中可变数量参数的位置关联,以实现更好的错误消息构造
- javascript - 我不能使用“element.style.top = window.scrollY”移动元素吗?
- android - 备份超过默认配额的安卓应用数据的可行策略