删除img标签里的width和height属性,并在img标签前后加一个br标签

发布时间 2023-07-14 10:01:22作者: 布都御魂
                # 提取img标签
                tree_img = etree.HTML(content)
                width = tree_img.xpath('//img//@width')[0]
                height = tree_img.xpath('//img//@height')[0]
                # 替换掉width=,和height=
                content = content.replace('height=', '').replace('width=', '').replace('"' + width + '"', '').replace(
                    '"' + height + '"', '')
                # 改成<p><br\><img src="1.jpg"><br\></p>
                img_list = re.findall('<img(.*?)>', content)
                for img in img_list:
                    img_old = '<img' + img + '>'
                    img_new = '<br/>' + img_old + '<br/>'
                    content = content.replace(img_old, img_new)
                content=content.replace('<p>','<br/><br/><p>').replace('</p>','</p><br/><br/>')