a = Question()
a.all_answer() 正常,结果打印到屏幕
但是我想 a.all_answer(write=True) 执行 if write == True:把结果写入文件
但 a.all_answer(True)报错:
TypeError: internal() takes exactly 1 argument (2 given)
a.all_answer(erite=True)也报错:
TypeError: internal() got multiple values for keyword argument 'write'
把装饰函数放到类中也一样。
代码高亮:
https://git.oschina.net/hgzhg/test/blob/master/decorator.pydef write_answer(pre=''):
def wrapper(F):
def internal(write):
if write == True:
result = F(write)
with open('/path/a.txt', 'w') as f:
f.write(result)
else:
print pre
F(write)
return internal
return wrapper
class Question(object):
@
write_answer('logging...')
def all_answer(self, write=False):
#下面代码部分直接忽略吧
answer_number = self.answer_number()
reg_img = r'
http://pic3.zhimg.com/\w+\.jpg'
if answer_number == 0:
print "This question is no answers"
else:
re_br = re.compile(r'<br/?>')
re_allmark = re.compile(r'<[^>]+>',re.S)
all_answer_list = self.soup.find_all( 'div', 'zm-item-answer ')
for each in all_answer_list:
print each.h3.get_text()
content = str(each.find('div', ' zm-editable-content clearfix'))
br2n = re.sub(re_br, '\n', content)
for s in br2n.split('\n'):
print re.sub(re_allmark, '', s)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/193260
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.