Skip to content

Instantly share code, notes, and snippets.

@dasdachs
Created March 17, 2016 07:14
Show Gist options
  • Save dasdachs/38428ad8b0f4c57a83c9 to your computer and use it in GitHub Desktop.
Save dasdachs/38428ad8b0f4c57a83c9 to your computer and use it in GitHub Desktop.
Python - get URLs from CSS
def get_urls(css_file):
"""
Get all URLs from a CSS file with regex pattern matching.
URI's, like data URIs are ignored and URLs might be relative.
:param css_file: a string representing the CSS file (use open()
and read() or a request library to get the string)
:return: list with retrived urls
"""
import re
urls = re.findall(r'url\(["\']?(?P<url>[^"\'\)].*?)["\']?[\)]', css_file)
return urls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment