本文整理汇总了Python中match.Match.fix_html方法的典型用法代码示例。如果您正苦于以下问题:Python Match.fix_html方法的具体用法?Python Match.fix_html怎么用?Python Match.fix_html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类match.Match
的用法示例。
在下文中一共展示了Match.fix_html方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fix_image
# 需要导入模块: from match import Match [as 别名]
# 或者: from match.Match import fix_html [as 别名]
def fix_image(self, content, recipe):
content = Match.fix_html(content=content, recipe_kind=recipe)
for img in re.findall(r'<img[^>]*', content):
if recipe not in [Type.sinablog_author, Type.cnblogs_author]:
# fix img
if img[-1] == '/':
img = img[:-1]
img += '>'
src = re.search(r'(?<=src=").*?(?=")', img)
if not src:
new_image = img + '</img>'
content = content.replace(img, new_image)
continue
else:
src = src.group(0)
if src.replace(' ', '') == '':
new_image = img + '</img>'
content = content.replace(img, new_image)
continue
src_download = HtmlCreator.fix_image_src(src)
if src_download:
if recipe in Type.zhihu and not src_download.startswith('http'):
# fix zhuanlan image href
src_download = src_download.split('.')[0]
filename = self.image_container.add('https://pic2.zhimg.com/'+src_download+'_b.jpg')
elif recipe in Type.generic:
filename = '' # TODO
else:
filename = self.image_container.add(src_download)
else:
filename = ''
new_image = img.replace('"{}"'.format(src), '"../images/{}"'.format(filename))
if recipe in Type.jianshu:
new_image = new_image.replace('data-original-src', 'temppicsr')
new_image = new_image.replace('src', 'falsesrc')
new_image = new_image.replace('temppicsr', 'src') # 应该有更好的方式, 暂时先这样写
new_image += '</img>'
elif recipe in Type.sinablog:
# 硬编码, 可以优化?写到fix_html函数中
new_image = new_image.replace('http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif',\
'../images/{}'.format(filename))
elif recipe in Type.zhihu:
new_image = new_image.replace('//zhstatic.zhihu.com/assets/zhihu/ztext/whitedot.jpg',
'../images/{}'.format(filename))
new_image += '</img>'
elif recipe in Type.cnblogs:
pass
content = content.replace(img, '<div class="duokan-image-single">{}</div>'.format(new_image))
return content