用Python替换html字符串

时间:2017-08-30 05:03:55

标签: python html

我一直在努力解决这个问题。我一直在尝试使用Python修改基于文件夹内容的html。但每当我尝试修改它时,整个事情就会崩溃。我是Python和HTML的新手。我创建了一个类,它将加载HTML文件和一些子函数来修改它:

class htmlfile(object):
    data = ""

    def openHTML(self, name="main.html"):
        # f = open('main.html')
        # for line in f:
        #     #print line.replace("Thinominal", "")
        with open(name) as myHtml:
            data = myHtml.read()
        self.data = data

    # def __repr__(self):
    #     return "Test()"
    def __str__(self):
        return self.data

    def replace(self, location, insert):
        self.data.replace(location, insert)

    def makeBackground(self, location):
        self.data.replace("backgrounds: [", "backgrounds: ["+ getPhotos2())

另一个函数来获取文件夹中的"内容" /照片列表,并返回稍后插入HTML中的字符串:

def getPhotos2(dir, type="jpg"):  # get Photo from a directory with a chosen Type. Default Type = jpg
    os.chdir(dir)
    data = ""
    for file in glob.glob("*.%s" % type):
        data = data + "{src: '%s/%s', fade: 2000}," % (dir, file)
    return data

但每当我运行此错误时,都会显示错误:

self = <__main__.htmlfile object>, self.data = '<!DOCTYPE html>\r\n<html lang="en">\r\n<head>\r\n <...\n</script>\r\n<!-- /Navbar-->\r\n\r\n</body>\r\n\r\n</html>', self.data.toString undefined, global getPhotos2 = <function getPhotos2>

我不明白。是不是python打开html作为字符串/文本文件?并打印它?就像你只做:

html = """ <head>Hello World</head>"""
print html

这会奏效。你也可以这样做:

html.replace("hello world", "BYE BYE")也是。那我在这里做错了什么?我错过了什么吗?任何建议将不胜感激。

这就是我调用函数的方式:

htmlfile = htmlfile()
htmlfile.openHTML()
htmlfile.makeBackgroud("background")
print htmlfile

我想能够替换Html的任何部分,就像它是一个STRING一样。例如: 这是手动填充的:

backgrounds: [
        {src: './img/nature1.jpg', fade: 2000},
        {src: './img/bw1.jpg', fade: 2000},
        {src: './img/portrait1.jpg', fade: 2000},
        {src: './img/portrait5.jpg', fade: 2000},
        {src: './img/portrait2.jpg', fade: 2000},
        {src: './img/portrait3.jpg', fade: 2000},
        {src: './img/portrait4.jpg', fade: 2000},
        {src: './img/forest.jpg', fade: 2000}

    ]

使用&#34; makeBackground&#34;函数,它将生成这些字符串并将其插入HTML中。如果我有一个包含coolImg.jpg的文件夹调用背景。使背景生成:{src: './img/coolImg.jpg', fade: 2000}并将其插入&#34;背景:&#34;部分。结果:

backgrounds: [
        {src: './img/nature1.jpg', fade: 2000},
        {src: './img/bw1.jpg', fade: 2000},
        {src: './img/portrait1.jpg', fade: 2000},
        {src: './img/portrait5.jpg', fade: 2000},
        {src: './img/portrait2.jpg', fade: 2000},
        {src: './img/portrait3.jpg', fade: 2000},
        {src: './img/portrait4.jpg', fade: 2000},
        {src: './img/forest.jpg', fade: 2000},
        {src: './background/coolImg.jpg', fade: 2000} <- THIS HERE
    ]

1 个答案:

答案 0 :(得分:0)

我想出了如何让它现在起作用。作为一个菜鸟,这主要是我的错。我忘了设置替换字符串的值来替换原始字符串。像这样:

self.data = self.data.replace(oldstring, newstring)

缺少“self.data =”。 我也忘了给出我的函数所要求的相同数量的参数。

def getPhotos2(dir, type="jpg"):  # get Photo from a directory with a chosen Type. Default Type = jpg
os.chdir(dir)
data = ""
for file in glob.glob("*.%s" % type):
    data = data + "{src: '%s/%s', fade: 2000}," % (dir, file)
return data

print "Content-type: text/html\n"


# html class
class htmlfile(object):
data = ""

def openHTML(self, name="main.html"):
    with open(name) as myHtml:
        data = myHtml.read()
    self.data = data

def __str__(self):
    return self.data

def replace(self, location, insert):
    self.data = self.data.replace(location, insert)
def makeBackground(self, location):
    self.data = self.data.replace("backgrounds: [", "backgrounds: ["+ getPhotos2(location))


htmlfile = htmlfile()
htmlfile.openHTML()
htmlfile.makeBackground("photo")
print htmlfile

这项工作,如果有人能发现任何错误,请随时告诉我。谢谢大家帮助^^