Python Forum
finding class with only part of the name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding class with only part of the name
#1
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
Reply
#2
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()
Reply
#3
Thank you :)

re.compile(^text) helped me :D
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020