在使用for循环时,要注意其所得的单个变量,是只读的,不能修改。
想要修改其值的话,只能借助其他变量实现。
示例代码如下:
for singleContent in soupContents: #singleContent is BeautifulSoup.Tag ...... if(recursive): ...... filteredSingleContent = singleContent; # here must use another tmp value to store changed value filteredSubContentList = removeSoupContentsTagAttr(filteredSingleContent.contents, ......); ...... filteredSingleContent.contents = filteredSubContentList; # here if use singleContent.contents = filteredSubContentList will cause error !!! #logging.debug("[%d] after filter, sub contents=%s", currentLevel, filteredSingleContent); filtedContents.append(filteredSingleContent); else: ......