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

lat*_*ish 3 python bs4

我有一个id为"img-cont"的div

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

我想用美丽的汤来提取背景图像中的网址.我该怎么做?

osh*_*ibr 5

你可以find_allfind第一场比赛.

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. 
Run Code Online (Sandbox Code Playgroud)