Python 分解类 BS4

我在按类删除标签时遇到问题,这是我的脚本:


from bs4 import BeautifulSoup


def 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 soup


def unwrapElements(soup, elementsToFind):

    elements = soup.find_all(elementsToFind)

    for element in elements:

        element.unwrap()



description_filter(str)


print(str)

它的查找跨度但不是从汤中删除......我做错了什么?


翻翻过去那场雪
浏览 92回答 1
1回答

达令说

它实际上从你的汤中去除了 span 但你并没有改变你的str价值而是打印旧的。所以你没有认出它。如果您想str在某些操作后更改您的设置,只需更改description_filter(str)为str = description_filter(str)print(str) // will print new str without span class akj-description-price
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python