这是一个使用BeautifulSoup的粗略示例,它建立在 David Robinson 的回答之上。装饰器使用传递给它的字符串作为对应于函数的字典的键。这是通过关键字参数传递给调用它的装饰函数。
import os
import sys
# Import System libraries
from copy import deepcopy
# Import Custom libraries
from BeautifulSoup import BeautifulSoup, Tag
page_base_str = \
'''
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<div id="body">
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</div>
</body>
</html>
'''
page_base_tag = BeautifulSoup(page_base_str)
def default_gen(*args):
return page_base_tag.prettify()
def test_gen(**kwargs):
copy_tag = deepcopy(page_base_tag)
title_change_locations = \
[
lambda x: x.name == u"title",
lambda x: x.name == u"h1"
]
title = kwargs.get("title", "")
if(title):
for location in title_change_locations:
search_list = copy_tag.findAll(location)
if(not search_list):
continue
tag_handle = search_list[0]
tag_handle.clear()
tag_handle.insert(0, title)
body_change_locations = \
[
lambda x: x.name == "div" and set([(u"id", u"body")]) <= set(x.attrs)
]
body = kwargs.get("body", "")
if(body):
for location in body_change_locations:
search_list = copy_tag.findAll(location)
if(not search_list):
continue
tag_handle = search_list[0]
tag_handle.clear()
tag_handle.insert(0, body)
return copy_tag.prettify()
page_gens = \
{
"TEST" : test_gen
}
def page_gen(name = ""):
def dec(func):
def inner_func(**kwargs):
kwargs["PAGE_FUNC"] = page_gens.get(name, default_gen)
return func(**kwargs)
return inner_func
return dec
@page_gen("TEST")
def test_page_01(**kwargs):
content = kwargs["PAGE_FUNC"](title = "Page 01", body = "Page 01 body")
return content
@page_gen("TEST")
def test_page_02(**kwargs):
content = kwargs["PAGE_FUNC"](title = "Page 02", body = "Page 02 body")
return content
@page_gen()
def a_page(**kwargs):
content = kwargs["PAGE_FUNC"]()
return content
print test_page_01()
print test_page_02()
print a_page()