爬取background-image:url中的图片链接

获取图片:

div class="img-thumb-5" style="background-image:url('/uploads/images/players/16113-1399107741.jpeg');"></div></a></div>
<div class="col pl-0">
import cssutils
from bs4 import BeautifulSoup

html = """<div class="image" style="background-image: url('/uploads/images/players/16113-1399107741.jpeg');" />"""
soup = BeautifulSoup(html)
div_style = soup.find('div')['style']
style = cssutils.parseStyle(div_style)
url = style['background-image']

>>> url
u'url(/uploads/images/players/16113-1399107741.jpeg)'
>>> url = url.replace('url(', '').replace(')', '')    # or regex/split/find/slice etc.
>>> url
u'/uploads/images/players/16113-1399107741.jpeg'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。