从样式中提取url:background-url:使用beautifulsoup而不使用正则表达式?

时间:2014-07-27 14:31:49

标签: python string web-scraping beautifulsoup

我有:

<div class="image" style="background-image: url('/uploads/images/players/16113-1399107741.jpeg');"

我想获取网址,但是如果不使用正则表达式我就无法做到这一点。它甚至可能吗?

到目前为止我的正则表达式解决方案是:

url = re.findall('\('(.*?)'\)', soup['style'])[0]

5 个答案:

答案 0 :(得分:4)

您可以尝试使用cssutils包。这样的事情应该有效:

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'

虽然您最终需要解析实际网址,但此方法应该更适应HTML中的更改。如果你真的不喜欢字符串操作和正则表达式,你可以用这种迂回方式拉出网址:

sheet = cssutils.css.CSSStyleSheet()
sheet.add("dummy_selector { %s }" % div_style)
url = list(cssutils.getUrls(sheet))[0]
>>> url
u'/uploads/images/players/16113-1399107741.jpeg'

答案 1 :(得分:2)

如何使用str.split

>>> style
'<div ... url(\'/uploads/images/players/16113-1399107741.jpeg\');"'
>>> style.split("('", 1)[1].split("')")[0]
'/uploads/images/players/16113-1399107741.jpeg'

答案 2 :(得分:1)

如果没有正则表达式,您可以使用str.find和str slice:

>>> s
"background-image: url('/uploads/images/players/16113-1399107741.jpeg');"
>>> s.find("('")
21
>>> s.find("')")
68
>>> s[21+len("('"):68]
'/uploads/images/players/16113-1399107741.jpeg'

但是,我觉得在你的情况下使用正则表达式更好。

答案 3 :(得分:0)

In [1]: s = "background-image: url('/uploads/images/players/16113-1399107741.jpeg');"

In [2]: start = s.find("url('")

In [3]: start
Out[3]: 18

In [4]: end = s.find("');")

In [5]: end
Out[5]: 68

In [6]: url = s[start+len("url('"):end]

In [7]: url
Out[7]: '/uploads/images/players/16113-1399107741.jpeg'

答案 4 :(得分:0)

from bs4 import BeautifulSoup
import re
html = """<div class="image" style="background-image: url('/uploads/images/players/16113-1399107741.jpeg');"""

soup = BeautifulSoup(html,'html.parser')
image_div = soup.find('div')['style']

ptr = re.search("http.*[)]",image_div) # regex to search url till ')'
print(image_div[ptr.start():ptr.end()-1]) # end() -1 to remove ')'