简体   繁体   English

Python获取所有CSS链接

[英]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. 我开始使用cssutils库,并且想做类似lxml.html库函数“ iterlinks()”的内容,总而言之,我只想获取css文件中的所有链接。 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 cssutils为此提供了一种方法-使用getUrls

It will iterate over a rule, or over the the entire stylesheet, and return all of the urls that it finds. 它将遍历一个规则或整个样式表,并返回找到的所有URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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