如何使用 BeautifulSoup 从内联样式中提取 CSS 属性 [英] How to pull out CSS attributes from inline styles with BeautifulSoup

查看:32
本文介绍了如何使用 BeautifulSoup 从内联样式中提取 CSS 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

<img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/>

我正在使用 beautifulsoup 来解析 html.是否可以在背景"css 属性中提取url"?

解决方案

您有几个选择 - 快速和肮脏或正确的方式.快速而肮脏的方式(如果更改标记很容易破坏)看起来像

<预><代码>>>>从 BeautifulSoup 导入 BeautifulSoup>>>进口重新>>>汤 = BeautifulSoup('<html><body><img style="background:url(/theRealImage.jpg) no-repate 0 0; height:90px; width:92px;") src="notTheRealImage.jpg"/></body></html>')>>>样式 = 汤.find('img')['style']>>>urls = re.findall('url((.*?))', style)>>>网址[u'/theRealImage.jpg']

显然,您必须使用它才能使其与多个 img 标签一起使用.

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

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?

解决方案

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

>>> 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']

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.

这篇关于如何使用 BeautifulSoup 从内联样式中提取 CSS 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆