正则表达式在组内获得匹配
发布时间:2020-12-14 05:47:15 所属栏目:百科 来源:网络整理
导读:我不知道以下是否可能. 我们假设我有以下文字: ul class="yes" liimg src="whatever1"/li liimg src="whatever2"/li liimg src="whatever3"/li liimg src="whatever4"/li/ulul class="no" liimg src="whatever5"/li liimg src="whatever6"/li liimg src="wh
我不知道以下是否可能.
我们假设我有以下文字: <ul class="yes"> <li><img src="whatever1"></li> <li><img src="whatever2"></li> <li><img src="whatever3"></li> <li><img src="whatever4"></li> </ul> <ul class="no"> <li><img src="whatever5"></li> <li><img src="whatever6"></li> <li><img src="whatever7"></li> <li><img src="whatever8"></li> </ul> 我想将ul中的每个img的src与class yes匹配. whatever1 whatever2 whatever3 whatever4 如何在一个正则表达式中加入这样的两个正则表达式? <ul class="yes">(.+?)</ul> <img src="(whatever.+?)"> 解决方法
众所周知,正则表达式难以用于解析类似XML的东西.更好地跳过这个想法并使用适当的HTML解析器滚动,例如,使用
BeautifulSoup4:
import bs4 html = """ <ul class="yes"> <li><img src="whatever1"></li> <li><img src="whatever2"></li> <li><img src="whatever3"></li> <li><img src="whatever4"></li> </ul> <ul class="no"> <li><img src="whatever5"></li> <li><img src="whatever6"></li> <li><img src="whatever7"></li> <li><img src="whatever8"></li> </ul> """ soup = bs4.BeautifulSoup(html) def match_imgs(tag): return tag.name == 'img' and tag.parent.parent.name == 'ul' and tag.parent.parent['class'] == ['yes'] imgs = soup.find_all(match_imgs) print(imgs) whatevers = [i['src'] for i in imgs] print(whatevers) 产量: [<img src="whatever1"/>,<img src="whatever2"/>,<img src="whatever3"/>,<img src="whatever4"/>] [u'whatever1',u'whatever2',u'whatever3',u'whatever4'] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读