Python Forum

Full Version: finding class with only part of the name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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:
Output:
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
In your case you could use something like this:
response.xpath('//div[contains(@class, "test-abc-")]').getall()
Thank you :)

re.compile(^text) helped me :D