python3 xpath解析html并修改后输出

前言:

平时我们都是对html文件进行解析后再取数据,用来做数据清洗。网上的xpath教程也很好有讲到怎么对html文件进行修改

这里我给出一个例子,保证简单易懂,摆脱手敲re正则表达式的烦恼.

demo的html文件。这里我暂且取名为111.html

py代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : html解析.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2020/6/16
#Refrence http://www.voidcn.com/article/p-ouvjtcvz-bty.html

from bs4 import BeautifulSoup
# from lxml import etree
from lxml import html

def main_func(out_file="ret.html"):
    with open("111.html", encoding="utf-8") as f:
        html_str = f.read()
    html_str = BeautifulSoup(html_str, "html.parser").prettify()
    html_xp = html.fromstring(html_str)
    for src in html_xp.xpath("//img[not(contains(@src,http))]"):
        old_src = src.xpath(./@src)[0]
        new_src = fun_path/name_dir/static/description/{old_src}
        src.attrib[src] = new_src
    ret = html.tostring(html_xp)
    with open(out_file,mode=wb+) as f:
        f.write(ret)
    print(f"处理完毕,输出文件为{out_file}")

if __name__ == __main__:
    main_func()

想要的效果:

将原html里面img标签下面的src没有http地址的链接,全部在前面加上我指定的路径进行修改后输出一个新的html文件

最后的得到的效果: ret.html

经验分享 程序员 微信小程序 职场和发展