在 Python 中使用 beautifulsoup 4,在表格中找到一个特定的字符串,然后提取文本



一天中的好时光!

在从事抓取项目时,我遇到了一些问题。 我必须从表中抓取字符串值,基于tr字符串进行搜索,如下所示:

span = list()
span.append({
"Price":soup.find("p", class_="classified__price").find("span",class_="sr-only").text,
"Kitchen":soup.find("th",text="Kitchen type").find_next(class_="classified-table__data").text
})

考虑到我是否将.text留在"厨房"键中值的末尾 - 但是什么也不打印

span = list()
span.append({
"Price":soup.find("p", class_="classified__price").find("span",class_="sr-only").text,
"Kitchen":soup.find("th",text="Kitchen type").find_next(class_="classified-table__data")
})

结果在

[{'Price': '410000€', 'Kitchen': <td class="classified-table__data">
Installed
</td>}]

非常感谢所有的帮助!

你想要的输出实际上在下一个元素下,要找到它,你可以使用.next_element方法:

span = list()
span.append({
"Price":soup.find("p", class_="classified__price").find("span",class_="sr-only").text,
"Kitchen":soup.find("th",text="Kitchen type").find_next(class_="classified-table__data").next_element.strip()
})
print(span)

输出:

[{'Price': '410000€', 'Kitchen': 'Installed'}]

最新更新