Reputation: 3369
If I get an element <section>
:
client = Mechanize.new
body = client.get("https://www.linternaute.fr/dictionnaire/fr/definition/perdu/")
section = body.search('.dico_definition')
and I want get all occurrences of <div class="dico_title_2">
only content in this <section>
I tried :
section.search('.dico_title_2')
but it gets <div class="dico_title_2">
of all pages and not only in html content in <section>
.
section.at('.dico_title_2')
gets <div class="dico_title_2">
in <section>
but only the first occurrence...
How can I get all occurrences?
I find may be the problem :
JavaScript : document.querySelectorAll('section.dico_definition > *')
returns 7 elements but data.search('section.dico_definition')
returns 4 elements...
Maybe a problem in interpretation of html?
How can I do in this case ?
Upvotes: 0
Views: 540
Reputation: 186
Try this:
client = Mechanize.new
data = client.get("https://www.linternaute.fr/dictionnaire/fr/definition/perdu/")
section = data.search('section//.dico_title_2')
section.first # This is the first element
^^^ This will find any tags with .dico_title_2
class nested within a section tag.
Note: Variable name body
could be confused with the <body> tag content, so I'd instead call it something like html_content
or data
.
Upvotes: 1