finding class with only part of the name - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: finding class with only part of the name (/thread-23902.html) |
finding class with only part of the name - zarize - Jan-22-2020 hi guys, Let's say that i have: 1st Case <div> <class: test-abc-1>2nd Case <div> <class: test-abc-2>How to tell python to look for every div, with class with begins with test-abc? So far i was very precise: find('div', {'class': 'test-abc-1'})but sometimes page can add random numbers to it's class so in this case the same element with the same kind of info can be found under different class RE: finding class with only part of the name - ThiefOfTime - Jan-23-2020 Hi, take a look at the documentation: https://docs.scrapy.org/en/latest/topics/selectors.html Search for the contains function. You can check if an attribute (for example a href) just contains a given string but is not matching it completely. For example: (Taken from the documentation) response.xpath('//a[contains(@href, "image")]/@href').getall()here from every "a"-tag the href element is taken, but only when the href contains the string "image". Which would match all of these: In your case you could use something like this:response.xpath('//div[contains(@class, "test-abc-")]').getall() RE: finding class with only part of the name - zarize - Jan-27-2020 Thank you :) re.compile(^text) helped me :D |