【发布时间】:2017-07-07 21:02:48
【问题描述】:
我使用 Beautifulsoup 解析 html 文件并检查文本是否为大写,在这种情况下,我将其更改为小写。当我将输出保存到新的 html 文件时,没有反映更改。谁能指出我做错了什么。
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)
【问题讨论】:
标签: python html string python-3.x beautifulsoup