简体   繁体   中英

Python get all the CSS links

I'm starting using the cssutils library and I want to do something like the lxml.html library function "iterlinks()", in a nutshell I just want to get all the links inside the css file. I thought on doing it myself but I dont know if this is well done:

sheet = cssutils.parseString("http://example.com/style.css")
links = []
for rule in sheet.cssRules:
    for child in rule.style.children():
        value = child.propertyValue
        for i in range(value.length):
            if isinstance(i, cssutils.css.URIValue):
                links.append(i)

I would like to know if there is a cleaner way to do it, even if there is an alternative library

cssutils has a method for this -- use getUrls

It will iterate over a rule, or over the the entire stylesheet, and return all of the urls that it finds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM