如何使用BeautifulSoup提取div的属性值

random_user

我有一个ID为“ img-cont”的div

<div class="img-cont-box" id="img-cont" style='background-image: url("http://example.com/example.jpg");'>

我想用漂亮的汤提取背景图片中的网址,该怎么办?

Oshribr

你可以在你find_all还是find第一个匹配。

import re 
soup = BeautifulSoup(html_str)
result = soup.find('div',attrs={'id':'img-cont','style':True})
if result is not None:
  url = re.findall('\("(http.*)"\)',result['style']) # return a list. 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章