一天中的好时光!
在从事抓取项目时,我遇到了一些问题。 我必须从表中抓取字符串值,基于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'}]