0
我使用Beautifulsoup解析html文件並檢查文本是否大寫,在這種情況下,我將它更改爲小寫。當我將輸出保存到新的html文件時,更改沒有被反映出來。有人能指出我做錯了什麼嗎?Python - 使用beautifulsoup保存更改
def recursiveChildren(x):
if "childGenerator" in dir(x):
for child in x.childGenerator():
name = getattr(child, "name", None)
if name is not None:
print(child.name)
recursiveChildren(child)
else:
if not x.isspace():
print (x)
if(x.isupper()):
x.string = x.lower()
x=x.replace(x,x.string)
if __name__ == "__main__":
with open("\path\) as fp:
soup = BeautifulSoup(fp)
for child in soup.childGenerator():
recursiveChildren(child)
html = soup.prettify("utf-8")
with open("\path\") as file:
file.write(html)