如何使用BeautifulSoup从内联样式中提取CSS属性

前端之家收集整理的这篇文章主要介绍了如何使用BeautifulSoup从内联样式中提取CSS属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的事情:
<img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/>

我正在使用beautifulsoup来解析HTML.有没有拉出“背景”css属性中的“url”?

@R_502_323@

你有几个选择 – 快速和肮脏或正确的方式.快速而肮脏的方式(如果更改标记会很容易破坏)看起来像
>>> from BeautifulSoup import BeautifulSoup
>>> import re
>>> soup = BeautifulSoup('<html><body><img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/></body></html>')
>>> style = soup.find('img')['style']
>>> urls = re.findall('url\((.*?)\)',style)
>>> urls
[u'/theRealImage.jpg']

显然,你必须使用它来使它与多个img标签一起使用.

正确的方法,因为我觉得有人在CSS字符串上使用正则表达式:),使用CSS解析器. cssutils,我刚刚在Google上找到并在PyPi上可用的库,看起来它可以完成这项工作.

原文链接:https://www.f2er.com/css/217302.html

猜你在找的CSS相关文章