Home > Software design >  How to use Regex in CSS Selector scrapy
How to use Regex in CSS Selector scrapy

Time:09-28

I need to get a ul tag by the class name but the class name has a lot of different combinations but it is always just two letters that changes. product-gallerytw__thumbs could be one and product-galleryfp__thumbs could be one. I need to know how to use a css selector that uses regex so that either of these could be found (or any other combination)

I can't use Xpath as the location changes

img_ul = response.css('.product-gallerytw__thumbs')
        
print(img_ul)

This is what I am trying to do but have not found a way to add regex inside the .css()

CodePudding user response:

You actually can use xpath:

img_ul = response.xpath("//*[contains(@class,'product-gallery')]")

or if you really need to specify everything but the two characters:

img_ul = response.xpath("//*[contains(@class,'product-gallery')][contains(@class,'__thumbs')]")

There is nothing a css selector can do that xpath can't. In fact css selectors are simply an abstraction of xpath selectors.

  • Related