如何使用 BeautifulSoup 从内联样式中提取 CSS 属性
我有这样的事情:
<img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/>
我正在使用 beautifulsoup 来解析 html。有没有办法提取“background”css属性中的“url”?
I have something like this:
<img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/>
I am using beautifulsoup to parse the html. Is there away to pull out the "url" in the "background" css attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几个选择——快速而肮脏的或正确的方式。快速而肮脏的方式(如果标记发生更改,这种方式很容易中断)看起来像
显然,您必须使用它才能使其与多个
img
标签一起使用。正确的方法是使用 CSS 解析器,因为我觉得建议某人在 CSS 字符串上使用正则表达式会很糟糕:)。 cssutils 是我刚刚在 Google 上找到的一个库,可以在 PyPi 上使用,看起来它可以完成这项工作。
You've got a couple options- quick and dirty or the Right Way. The quick and dirty way (which will break easily if the markup is changed) looks like
Obviously, you'll have to play with that to get it to work with multiple
img
tags.The Right Way, since I'd feel awful suggesting someone use regex on a CSS string :), uses a CSS parser. cssutils, a library I just found on Google and available on PyPi, looks like it might do the job.