我在按類刪除標(biāo)簽時遇到問題,這是我的腳本: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 回答

達(dá)令說
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個贊
它實(shí)際上從你的湯中去除了 span 但你并沒有改變你的str價(jià)值而是打印舊的。所以你沒有認(rèn)出它。如果您想str在某些操作后更改您的設(shè)置,只需更改description_filter(str)為
str = description_filter(str)
print(str) // will print new str without span class akj-description-price
添加回答
舉報(bào)
0/150
提交
取消