同是str类型,为什么一个有返回值,一个没有?

lioujian47 lioujian47 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
两次调用getcontent(),只有一次能输出:(



def body(url1,url2):
try:
txt = urllib.urlopen(url1).read()
except Exception, e:
txt = urllib.urlopen(url2).read()
txt = unicode(txt,"gbk")
parser = html2txt()
parser.feed(txt)
parser.close()
#print type(parser.text) #<type 'unicode'>
return parser.text
def cr2n(txt):
#data = re.sub('(\s|\xa0)+','',txt.encode("gbk"))
#data = data.replace('<NEWLINE>',"\n").strip()
p = re.compile(r'(。|!|……|】)')
t = p.sub('。\n',txt.encode("gbk"))
#print type(t) # t is str
return t
def getcontent(ar,txt):
ctent = re.findall(ar,txt)
for i in ctent:
print i

url1 = 'http://docs.google.com/Doc?id=d8b2mxm_107cc3mgqgr' #
url2 = 'http://docs.google.com/Doc?id=d8b2mxm_107cc3mgqgr' #暂时用一样的

test = '需要匹配这个。' # test是str
ar = '匹配'

k = cr2n(body(url1,url2)) #k 是 str
getcontent(ar,k)#这个没有输出:(
getcontent(ar,test) #这个工作正确

...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
lioujian47
脚本语言 37,737 成员 34,208 内容