【问题标题】:Is there a way to extract the url of images my my css file in python? [duplicate]有没有办法在python中提取我的css文件中的图像的url? [复制]
【发布时间】:2021-05-07 06:12:27
【问题描述】:

我正在尝试获取 css 文件中使用的所有图像的 url。例如,这是我的 css 文件的一部分:

sitewrap {
    width: 890px;
    margin: 0 auto;
    background: #ffffff url(../images/bg_sitewrap.gif) repeat-y;
}

如何只接收它的 ../images/bg_sitewrap.gif 部分,最好在 python 中?

提前感谢您的帮助!

【问题讨论】:

    标签: python css


    【解决方案1】:

    您可以在这里尝试使用re.findall:

    css = """sitewrap {
        width: 890px;
        margin: 0 auto;
        background: #ffffff url(../images/bg_sitewrap.gif) repeat-y;
    }"""
    
    urls = re.findall(r'\burl\((.*?)\)', css)
    print(urls)  # ['../images/bg_sitewrap.gif']
    

    此答案假定您希望在 CSS 脚本中捕获 url(...) 的每个实例。如果没有,那么您可能需要使用 CSS 解析器/库,然后在管道中稍后使用正则表达式来查找 URL。

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多