flutter解析html的图片和链接
html链接内容如下:
"<p><a href="https://www.hihonor.com/cn/m/notice-14658" rel="nofollow"><img alt="750.png" src="https://hshop.honorfile.com/pimages/detailImg/2023/08/31/C757CDF3D975230D36F9176D41A903768F42A8D8198D66CB.png" /></a></p>"
pubspec.yaml文件依赖:
dependencies:
html: ^0.15.0
import 'package:html/parser.dart'; var document = parse(detail);///其中detail为html获取得到的字符串文本 document.getElementsByTagName("p").forEach((element) { var href=element.querySelector('a')?.attributes['href']; var imgSrc=element.querySelector('img')?.attributes['src']; LogI("element img src = $imgSrc , href=$href"); });
打印结果:
element img src = https://hshop.honorfile.com/pimages/detailImg/2023/08/31/C757CDF3D975230D36F9176D41A903768F42A8D8198D66CB.png , href=https://www.hihonor.com/cn/m/notice-14658