我需要替换我的html页面的数据内容中的一些字符串。我不能直接使用替换函数,因为我只需要更改数据部分。它不应该修改任何标签或属性。我为此使用HTMLParser
。但我坚持写回档案。使用HTMLParser
我可以解析并获取数据内容,我将在其中进行必要的更改。但如何把它放回我的html文件?python HTMLParser替换html文件中的一些字符串
请帮忙。这是我的代码:
class EntityHTML(HTMLParser.HTMLParser):
def __init__(self, filename):
HTMLParser.HTMLParser.__init__(self)
f = open(filename)
self.feed(f.read())
def handle_starttag(self, tag, attrs):
"""Needn't do anything here"""
pass
def handle_data(self, data):
print data
data = data.replace(",", "&sbquo")
请妥善缩进代码。 –