我在按类删除标签时遇到问题,这是我的脚本:from bs4 import BeautifulSoupdef description_filter(description): soup = BeautifulSoup(description, "lxml") for span in soup.select('.akj-description-price'): print(span) print(soup) span.decompose() unwrapElements(soup, "html") unwrapElements(soup, "body") return soupdef unwrapElements(soup, elementsToFind): elements = soup.find_all(elementsToFind) for element in elements: element.unwrap()description_filter(str)print(str)它的查找跨度但不是从汤中删除......我做错了什么?
1 回答
达令说
TA贡献1821条经验 获得超6个赞
它实际上从你的汤中去除了 span 但你并没有改变你的str价值而是打印旧的。所以你没有认出它。如果您想str在某些操作后更改您的设置,只需更改description_filter(str)为
str = description_filter(str)
print(str) // will print new str without span class akj-description-price
添加回答
举报
0/150
提交
取消