Home  >  Q&A  >  body text

Python how to extract all ID names and CLASS names from css files

There is a requirement to extract all ID names and CLASS names from the css file into two lists

Can this function be accurately implemented using regular expressions?

classValues = re.findall(r'\.([\w_-]+)',content) 

idValues = re.findall(r'#([\w_-]+)',content) 

But there will be many more values ​​that do not belong to ID and class, such as the following situation:

background-color: #fff; 
-webkit-box-shadow: 0 0 2px 0 rgba(31, 31, 31, 0.07);
扔个三星炸死你扔个三星炸死你2662 days ago1166

reply all(1)I'll reply

  • 代言

    代言2017-07-05 10:37:41

    re.findall('\.(.+){', content) 
    re.findall('#(.+){', content)
    

    reply
    0
  • Cancelreply